2014-01-21 47 views
0

我有一个未排序的排序列表和另一个排序顺序排列的列表。我需要添加一个删除按钮来从原始订单和排序顺序中删除单词,但要使用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())); 

    } 
+1

为什么你需要使用'的binarySearch '? – chrylis

+0

你只能在有序列表中使用'binarySearch',所以如果你想保持你的列表不被排序,只需使用'list.contains(String)'。 – alicjab

+0

[Burrows-Wheeler Transform](http://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform)涉及可逆排序。但它不会帮助您进行二分查找。 –

回答

4

使用Map<String, Integer> songToIndexMap来存储每首歌曲的索引。

然后就去做:

Integer index = songToIndexMap.remove(titleArtistInput.getText()); 
if(index != null) { // the song has been found! 
    song.remove(index); 
} 

二进制搜索O(log n)而在HashMapremove/getO(1)

+0

我只是一个初学者程序员是否有另一种方式来做到这一点? – user3009505

0

我认为,创建对列表是个好主意。一对保持位置,另一个保持价值。 look this question。您仍然可以根据您的价值对列表进行排序。

0

如果你想保留原来的顺序并且有一个快速的搜索时间,你可以组合一个Map和一个ArrayList。

对于每个项目,将它添加到ArrayList中,然后将它与列表的索引一起添加到地图中,使用作为关键字的字段作为顺序。

例子:

ArrayList<Song> originalSort = new ArrayList<>(); 
    Map<String, Song> theMap = new HashMap<>(); //we will sort by name of song 

    //add some items 
    Song song = new Song(); 
    song.author = "thatMan"; 
    song.name = "someTitle"; 
    //the index will be the index within the ArrayList 
    song.index = originalSort.size(); 

    originalSort.add(song); 
    theMap.put(song.name, song); 

    //... 

    //iterate with the original order: 
    for (Song song : originalSort) { 
     System.out.print(song.name); 
    } 

    //fast search and remove 
    song = theMap.remove(song.name); 
    originalSort.remove(song.index); //ArrayList has a fast acces by index 

如果它是用于测试目的,你可以简单地通过使列表的副本进行排序之前的原始顺序存储:

List listBackup = originalList.clone(); //remember to use clone method or you will be pointing to the same intance