2012-08-24 139 views
2

我正在嘗試搜索數組列表以查找值(可能會重複出現)並刪除該值的所有實例。我也想從一個單獨的數組列表中刪除處於相同位置的值。 ArrayLists都是ArrayList<String>Java ArrayList搜索和刪除

比如我在ArrayList2尋找號碼5:

ArrayList 1  ArrayList2 
cat    1 
pig    2 
dog    5 
chicken   3 
wolf    5 

一旦我找到了5號,在這兩個位置,我想從ArrayList1去除狗與狼。我的代碼沒有錯誤,但它似乎並沒有真正刪除我所要求的。

//searching for 
String s="5"; 
//for the size of the arraylist 
for(int p=0; p<ArrayList2.size(); p++){ 
//if the arraylist has th value of s 
if(ArrayList2.get(p).contains(s)){ 
    //get the one to remove 
    String removethis=ArrayList2.get(p); 
    String removetoo=ArrayList1.get(p); 
    //remove them 
    ArrayList2.remove(removethis); 
    ArrayList1.remove(removetoo); 
    } 
} 

當我打印arrayLists時,它們看起來基本沒有改變。任何人看到我做錯了什麼?

+1

你不想使用'Map '嗎?這將大大簡化這一過程。 – Makoto

+0

我不知道Map ...我會看到我能找到的東西。謝謝 – Stephopolis

+0

贊同@Makoto:爲什麼要維護兩個平行的列表?其他一些數據結構然後列表可能更合適 – bpgergo

回答

7

當你都循環,並從一個數組刪除項目,你寫的算法不正確,因爲它跳過下一每次移除後的項目(由於您增加p的方式)。考慮這個選擇:

int s = 5; 
int idx = 0; 

while (idx < ArrayList2.size()) 
{ 
    if(ArrayList2.get(idx) == s) 
    { 
    // Remove item 
    ArrayList1.remove(idx); 
    ArrayList2.remove(idx); 
    } 
    else 
    { 
    ++idx; 
    } 
} 
+0

這個解決方案工作得很好!我知道如何在迭代時更改陣列大小。非常感謝! – Stephopolis

1

如果你想遍歷集合,並刪除同一集合的元素,那麼你就必須使用一個Iterator,如:

List<String> names = .... 
List<Integer> numbers = .... 
int index = 0; 
Iterator<String> i = names.iterator(); 
while (i.hasNext()) { 
    String s = i.next(); // must be called before you can call i.remove() 
    if (s.equals("dog"){ 
     i.remove(); 
     numbers.remove(index); 
    } 
    index++; 
} 

編輯

在你的情況,您必須手動增加一個變量才能從另一個列表中刪除項目。

0

我覺得包含方法比較兩個對象。但是,對象「s」與ArrayList中的對象不同。你應該使用類型數組(即ArrayList中),並確保每個比較對象的值,而不是對象本身...

+1

我不知道我理解。你能否進一步解釋? – Stephopolis

+0

問題是,值5的對象「s」與列表中值爲5的對象的實例不同。 contains方法返回true,如果兩個對象相同,則它是同一個實例。因此它在你的代碼中總是返回false,並且這些對象不會被刪除。該解決方案已被許多其他人撰寫。使用類型化列表並使用equals方法而不是普通ArrayList幷包含方法。 – Sharg

0

你應該聲明您的列表如下 -

List<String> list1 = new ArrayList<String>(); 
//... 
List<Integer> list2 = new ArrayList<Integer>(); 
//... 

,取而代之的contains方法使用equals方法。

另外要同時遍歷列表使用Iterator你可以得到如下刪除 -

Iterator<String> it1 = list1.iterator(); 
Iterator<Integer> it2 = list2.iterator(); 

//... 
0

你可能要檢查的ArrayList indexOf()方法,但你必須從列表中刪除,而在它的元素進行迭代時要小心。

1

你可以使用兩個迭代器:

Iterator<String> i1 = arrayList1.iterator(); 
Iterator<Integer> i2 = arrayList2.iterator(); 
while (i1.hasNext() && i2.hasNext()) { 
    i1.next(); 
    if (i2.next() == s) { 
    i1.remove(); 
    i2.remove(); 
    } 
} 

雖然作爲已經指出的是,它可能會更容易使用的地圖。

+0

對於地圖+1和'i1.hasNext()&& i2.hasNext()' – bpgergo

0

這裏有一個簡單的解決方案:

List<Integer> origNums = new ArrayList<Integer>(nums); 
Iterator<String> animalIter = animals.iterator(); 
Iterator<Integer> numIter = nums.iterator(); 

while (animalIter.hasNext()) { 
    animalIter.next(); 

    // Represents a duplicate? 
    if (Collections.frequency(origNums, numIter.next()) > 1) { 

     // Remove current element from both lists. 
     animalIter.remove(); 
     numIter.remove(); 
    } 
} 

System.out.println(animals); // [cat, pig, chicken] 
System.out.println(nums); // [1, 2, 3] 
0

我真同意,使用地圖也許更有利。如果您只是使用ArrayList2的值進行搜索,那麼您有一個鍵的多個值。例如,5指狗和狼。所以5.

HashMap aMap = HashMap(); 

ArrayList key5 = new ArrayList(); 

key5.add("dog"); 
key5.add("wolf"); 

aMap.put(5, key5); 

當你需要刪除所有值5,你做

aMap.remove(5); 

它將刪除包含狗列表 - 爲此,您可以添加值的列表,以關鍵和狼。