【问题标题】:Why isn't Collections.binarySearch outputting consistently为什么 Collections.binarySearch 输出不一致
【发布时间】:2015-06-19 05:53:14
【问题描述】:

所以我正在尝试为学校编写一个程序。这样做的目的是列出用户收藏的 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);
    }
} 

【问题讨论】:

  • 不确定您要的是什么。

标签: java


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    相关资源
    最近更新 更多