2013-02-04 109 views
1

如何比較兩個陣列列表?我有一個sdCoverList數組和thumbnailList數組。比較兩個陣列列表並從中刪除多餘的元素

02-04 11:05:05.210: I/System.out(4035): HASHED THUMBNAIL[-2122410790, 2043473787, 1914391068, 1785308349, 1656225630, 1527142911, 1398060192, 1268977473, 1139894754, 1010812035, 1242943301, 1113860582, 984777863, 855695144, 726612425, 597529706, 468446987, 339364268, 210281549] 
02-04 11:05:05.210: I/System.out(4035): SD COVER LIST[-2122410790, 2043473787, 1914391068, 1785308349, 1268977473, 1656225630, 1527142911, 1139894754, 1398060192, 1010812035, 1242943301, 1113860582, 984777863, 855695144, 726612425, 597529706, 468446987, 339364268, 210281549, 717409028] 

在我的SD蓋名單,我有我想要刪除掉額外的價值,但首先我得先用thumbnailList陣列比較。如果兩者都具有相同的元素,則不要執行任何操作,但是如果有額外的內容,請將其從SD卡中移除。

我的數組列表

爲sdCoverList:

// CHECK DIRECTORY BEFORE DELETING THEM 
      File strPath = new File(Environment.getExternalStorageDirectory() + folderName+"/Covers"); 
      File yourDir = new File(strPath, ""); 
      if(yourDir.length()!=0) 
      for (File f : yourDir.listFiles()) { 
       if (f.isFile()) 
       { 
        String name = f.getName(); 
        sdCoverList.add(name); 
        //System.out.println(sdCoverList); 
       } 

爲hashedthumbnail

for (int a=0;a<=thumbnailList.size()-1;a++) 
     { 
      String hashed = String.valueOf(thumbnailList.get(a).hashCode()); 
      Log.d("hashed",hashed); 
      hashedThumbnail.add(hashed); 
     } 
+0

一個不錯的答案是[這裏](http://stackoverflow.com/a/919420/1265724) –

+0

如果我已經有一個arraylist,我該如何使用它的集合? – user1234555

+0

在這裏發佈您的代碼 –

回答

7
List<String> sdCoverList = new ArrayList<String>(); 
sdCoverList.add("-2122410790"); 
sdCoverList.add("2043473787"); 
sdCoverList.add("717409028"); 

List<String> hashedThumbnail = new ArrayList<String>(); 
hashedThumbnail.add("-2122410790"); 
hashedThumbnail.add("2043473787"); 

System.out.println("sdCover List: " + sdCoverList); 
System.out.println("hashedThumbnail List: " + hashedThumbnail); 


List<String> temp = new ArrayList<String>(); 
temp.addAll(sdCoverList); 
temp.removeAll(hashedThumbnail); 

System.out.println("temp List: " + temp); 

sdCoverList.removeAll(temp); 

System.out.println("sdCover List: " + sdCoverList); 

的輸出將是

sdCover List: [-2122410790, 2043473787, 717409028] 
hashedThumbnail List: [-2122410790, 2043473787] 
temp List: [717409028] 
sdCover List: [-2122410790, 2043473787] 
+0

非常感謝你的幫助:) – user1234555

+0

我有更多然後50000條記錄在數組中,如果我想添加所有和removeAll應用程序卡住在那種情況下我什麼做?? –

+0

addAll方法工作,但刪除所有沒有 –

相關問題