所以我正试图为学校写一个程序。它的目的是列出用户在其集合中的CD列表。程序需要以CDs的原始顺序(CDS)和按字母排序的顺序(CDSS)显示CDS列表。信息必须存储在数组列表中。我还必须使所使用的程序能够添加和删除程序。
当我输入歌曲“披头士-艾比路”时,我以为它会从collections.binarySearch输出一个1,但相反,它会输出-1倍,然后它输出1's,但它似乎适用于其他歌曲。
我也不能删除一首歌,因为它会产生错误。
谢谢你的帮助
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);
}
} 发布于 2015-06-18 21:51:08
从您已经说过的情况来看,CDS没有按任何特定的顺序排序。Collections.binarySearch在其Javadoc中明确指出,只有当它收到的列表已经排序时,它才能工作。
而不是使用Collections.binarySearch,您将不得不使用CDS.indexOf(inputSong)并接受线性搜索。
https://stackoverflow.com/questions/30926370
复制相似问题