2014-01-21 80 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