Java 集合的二进制搜索不起作用
我不熟悉JAVA和Netbeans,这是我必须做的: 用户可以在输入框中输入标题CD,然后按删除按钮将CD从列表中删除。如果收藏中不存在CD,则发件箱中会显示一条消息,说明此情况。 这就是我所做的:Java 集合的二进制搜索不起作用,java,arrays,netbeans,binary-search,Java,Arrays,Netbeans,Binary Search,我不熟悉JAVA和Netbeans,这是我必须做的: 用户可以在输入框中输入标题CD,然后按删除按钮将CD从列表中删除。如果收藏中不存在CD,则发件箱中会显示一条消息,说明此情况。 这就是我所做的: ArrayList <String> songs = new ArrayList(); Collections.addAll(songs, "Metric - Fantasies", "\nBeatles - Abbey Road", "\nPearl Jam - Ten", "\nDo
ArrayList <String> songs = new ArrayList();
Collections.addAll(songs, "Metric - Fantasies", "\nBeatles - Abbey Road", "\nPearl Jam - Ten", "\nDoors - Alive", "\nThe Rolling Stones - Gimme Shelter\n");
int remove = Collections.binarySearch(songs, artistinput.getText());
if (remove < 0)
{
output.setText("That CD does not exist in the collection, please try again");
}
else if (remove >= 0)
{
boolean delete=songs.remove(artistinput.getText());{
output.setText("Original Songs \n" +delete);
这是我唯一没有弄明白的部分。这不起作用,因为它只显示输出。SetText如果集合中不存在CD,请重试;每次我键入歌曲并按“删除”。感谢您的帮助,并提前向您表示感谢 使用java 8,您可以尝试以下方法:
List<String> songs = new ArrayList<String>();
int size = songs.size();
Collections.addAll(songs, "Metric - Fantasies", "\nBeatles - Abbey Road", "\nPearl Jam - Ten", "\nDoors - Alive", "\nThe Rolling Stones - Gimme Shelter\n");
songs.removeIf(o->o.equalsIgnoreCase(artistinput.getText()));
int arSize = songs.size();
if(size != arSize){
System.out.println("Original Songs \\n" + (size-arSize));
}else System.out.println("That CD does not exist in the collection, please try again");
为了使用binarysearch,您必须确保数组是有序的。你没有对你的歌曲进行分类,所以你没有得到预期的结果。我不知道您为什么要为歌曲名添加\n
另一种解决方案是先对数组排序。详情如下:
ArrayList <String> songs = new ArrayList();
Collections.addAll(songs, "Metric - Fantasies", "\nBeatles - Abbey Road", "\nPearl Jam - Ten", "\nDoors - Alive", "\nThe Rolling Stones - Gimme Shelter\n");
Collections.sort(songs);//////sort the songs
int remove = Collections.binarySearch(songs, artistinput.getText());
if (remove < 0)
{
output.setText("That CD does not exist in the collection, please try again");
}
else if (remove >= 0)
{
boolean delete=songs.remove(artistinput.getText());{
output.setText("Original Songs \n" +delete);
}
简单快捷的方法。我改变了你的输入和输出系统,但我猜这对你的班级来说是好的
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class dumbshit{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
ArrayList<String> songs = new ArrayList<String>();
songs.add("Metric - Fantasies");
songs.add("Beatles - Abbey Road");
songs.add("Pearl Jam - Ten");
//etc
//this can obviously be done better
Collections.sort(songs);
int index = Collections.binarySearch(songs, scan.nextLine());
if (index < 0)
{
System.out.println("That CD does not exist in the collection, please try again");
}
else if (index >= 0)
{
songs.remove(index);
for(int i = 0; i < songs.size(); i++){
System.out.println( songs.get(i) );
}
}
}
}
您的歌曲未排序。并且它包含换行符\n,为什么?还有,是吗?医生,您不太可能从您的输入中\n获得文本。这不是我,即使我们的问题有点相同,所以它显示在新行上,因为我使用的是textfield,而不会显示在新行上。所以,这就是我的老师建议我做的