为什么没有Collections.binarySearch一致输出

时间:2015-06-18 21:48:19

标签: java

所以我正在尝试为学校写一个程序。其重点是制作用户在其集合中具有的CD列表。程序需要以原始顺序(CDS)和按字母顺序排序的顺序(CDSS)显示CD列表。信息必须存储在数组列表中。我还必须使用能够添加和删除程序。

当我进入歌曲" Beatles - Abbey Road"时,我认为它会从collections.binarySearch输出1,但是它输出-1两次然后输出1'秒。它似乎适用于其他歌曲。

我也无法删除歌曲,因为它会产生错误

感谢您的帮助

private void buttonInitializeActionPerformed(java.awt.event.ActionEvent evt) {                                                 
    //this is where the original songs are added(when the initialize button is pressed)
    Collections.addAll(CDS, "Metric - Fantasies", "Beatles - Abbey Road", "Pearl Jam - Ten", "Doors - Alive", "The Rolling Stones - Gimme Shelter");
    Collections.addAll(CDSS, "Metric - Fantasies", "Beatles - Abbey Road", "Pearl Jam - Ten", "Doors - Alive", "The Rolling Stones - Gimme Shelter");
    //once the initialize button is pressed the other buttong will be able to be enabled
    buttonDisplay.setEnabled(true);
    buttonRemove.setEnabled(true);
    buttonAdd.setEnabled(true);
    buttonInitialize.setEnabled(false);
}                                                

private void buttonDisplayActionPerformed(java.awt.event.ActionEvent evt) {                                              
    outputSongList.setText("Original Order");

    int condition = 0;
    //condition is the value thatis increased by 1 every time each of these while loops are ran. 
    //from 1 to however many objects there are in the either the CDS or CDSS array list
    //the extra S in CDSS stands for sorted
    while(condition < CDS.size()){
        outputSongList.setText(outputSongList.getText() + "\n" + CDS.get(condition));//writing the contents of the array list to the text area
        condition++;
    }
    outputSongList.setText(outputSongList.getText() + "\n\n\nSorted Order");

    Collections.sort(CDSS, String.CASE_INSENSITIVE_ORDER);//this sorts the CDSS array in alphabetical order

    condition = 0;

    while(condition < CDSS.size()){
        outputSongList.setText(outputSongList.getText() + "\n" + CDSS.get(condition));//the same as the previous while loop
        condition++;
    }
    buttonDisplay.setEnabled(false);//disables the display button so the user knows that all information is displayed
}                                             

private void buttonAddActionPerformed(java.awt.event.ActionEvent evt) {                                          
    String inputSong = enterSong.getText();//getting the string that the user typed into the enterSong text field

    if(Collections.binarySearch(CDS, inputSong) < 0){//this checks if the inputted song is in the arraylist
        CDS.add(inputSong);                             //if it is, the outputted number will be 1...i thought
        CDSS.add(inputSong);                            //if it isn't the numer will be less than 0
        Collections.sort(CDSS);              //this if statement will run if the inputted song isn't already in the arrays
        buttonDisplay.setEnabled(true);                 
    }
}                                         

private void buttonRemoveActionPerformed(java.awt.event.ActionEvent evt) {                                             
    String inputSong = enterSong.getText();

    if(Collections.binarySearch(CDS, inputSong) > -1){//if the inputted song is in the array this line will run
        CDS.remove(Collections.binarySearch(CDS, inputSong));
        CDSS.remove(Collections.binarySearch(CDS, inputSong));
        buttonRemove.setEnabled(false);
        buttonDisplay.setEnabled(true);
    }
} 

1 个答案:

答案 0 :(得分:3)

根据您所说的,CDS未按任何特定顺序排序。 Collections.binarySearch在其Javadoc中明确指出,只有在收到的列表已经排序后,它才有效。

您必须使用Collections.binarySearch并接受线性搜索,而不是使用CDS.indexOf(inputSong)