我有一个未排序的排序列表和另一个排序顺序排列的列表。我需要添加一个删除按钮来从原始订单和排序顺序中删除单词,但要使用binarySearch删除我需要对原始订单进行排序。但我需要保持它未排序...使用binarySearch从ArrayList中删除单词
int songIndex = Collections.binarySearch(song, titleArtistInput.getText());
int sortedSongIndex = Collections.binarySearch(sortedSong, titleArtistInput.getText());
//To test the values.
System.out.println(songIndex + " " + sortedSongIndex);
if (sortedSongIndex < 0)
{
titleArtistOutput.setText("That CD does not exist in the collection, please try again");
}
else if (sortedSongIndex >= 0)
{
sortedSong.remove(sortedSongIndex);
Collections.sort(song);
song.remove(Collections.binarySearch(song, titleArtistInput.getText()));
}
有没有方法恢复Collections.sort?或者没有对歌曲ArrayList进行排序的任何方式?编辑: 我得到它自己工作!最后。
int sortedSongIndex = Collections.binarySearch(sortedSong, titleArtistInput.getText());
//if the Collections.binarySearch is negative (song not found), it will output
//"That CD does not exist in the collection, please try again", if the sortedSongIndex is positive
//(the song had been found!) and will remove the indexOf titleArtistInput.getText() from the ArrayLists
if (sortedSongIndex < 0)
{
titleArtistOutput.setText("That CD does not exist in the collection, please try again");
}
else if (sortedSongIndex >= 0)
{
sortedSong.remove(sortedSong.indexOf(titleArtistInput.getText()));
song.remove(song.indexOf(titleArtistInput.getText()));
}
为什么你需要使用'的binarySearch '? – chrylis
你只能在有序列表中使用'binarySearch',所以如果你想保持你的列表不被排序,只需使用'list.contains(String)'。 – alicjab
[Burrows-Wheeler Transform](http://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform)涉及可逆排序。但它不会帮助您进行二分查找。 –