2016-11-28 77 views
-2

我是JAVA和Netbeans的新手,這是我必須做的:
用戶可以在輸入框中輸入標題CD,然後通過按刪除按鈕。如果集合中不存在該CD,則可以在發件箱中顯示一條消息來說明這一點。
這是我做了什麼:集合的BinarySearch不起作用

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); 

這是我還沒有想通了該計劃的一部分。這不起作用,因爲每次我輸入歌曲並按'刪除'時它只顯示output.setText("That CD does not exist in the collection, please try again");。任何幫助表示感謝,並提前謝謝你!

+0

您的歌曲沒有排序。它包含換行符'\ n',爲什麼?另外,是[這](http://stackoverflow.com/questions/40835111/difficulty-with-collections-binarysearch/40835224?noredirect=1#comment68887977_40835224)你呢? –

+0

醫生不太可能會從輸入中用'\ n'獲得文本。 –

+0

多數民衆贊成那不是我即使我們的問題是有點相同 –

回答

0

與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"); 
0

爲了使用binarysearch你必須確保數組是有序。你還沒有排序songs,所以你不會得到你的預期結果。我不知道爲什麼你爲歌曲名稱添加\n


另一種解決方案是先排序數組。 AS如下:

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); 
    } 
+0

添加\ n所以它顯示在新行,因爲我使用的文本字段,並沒有顯示在新行。所以,這是我老師建議我做的 –

0

簡單快速的方法來做到這一點。我改變了你的輸入和輸出系統,但我猜這對你的班級來說很好。

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)); 
     } 
    } 
} 
}