简体   繁体   English

如何删除两个不同的ArrayLists中的两个相同的元素?

[英]How to remove two of the same elements in two different ArrayLists?

Just started to learn Java and have a question on how I can remove the common elements from two different Arraylists? 刚开始学习Java,并对如何从两个不同的Arraylist中删除通用元素有疑问?

My thought process went with: 我的思考过程伴随着:

 for(int i =0; i<playerOneInputArray.size(); i++) {
       if(playerOneInputArray.contains(playerTwoInputArray.get(i))){
           playerOneInputArray.remove(playerTwoInputArray.get(i));

       }
   }

however, when I check the out put i get: 但是,当我检查输出时,我得到:

Player One! Choose your word!
hello
Player Two! Choose your word!
hellow
[h, e, l, l, o]
[h, e, l, l, o, w]
[e, l]

which I expected to only get a [w] in my new playerOneInputArray 我希望在新的playerOneInputArray中只得到一个[w]

what's going on?? 这是怎么回事?? :C :C

The problem is that you are concurrently modifying a data structure. 问题是您正在同时修改数据结构。 Specifically, you are editing an ArrayList that you are iterating over. 具体来说,您正在编辑要遍历的ArrayList。

Consider the simplified problem of removing any even numbers from an arrayList. 考虑从arrayList中删除任何偶数的简化问题。 You may consider using this approach: 您可以考虑使用这种方法:

public void removeEvens(ArrayList<Integer> arr){
    for(int i = 0; i < arr.size(); i++){
        if(arr.get(i) % 2 == 0) arr.remove(i);
    }
}

But there is an issue with this approach. 但是这种方法存在问题。 Consider the sample input: 考虑样本输入:

1, 3, 4, 6, 7, 8

When we hit i = 2 , we correctly remove 4 . 当我们达到i = 2 ,我们正确地删除了4 However, at the next iteration ( i = 3 ), the arrayList now looks like: 但是,在下一次迭代( i = 3 )时,arrayList现在看起来像:

1, 3, 6, 7, 8

So we are checking 7 , not 6 . 因此,我们正在检查7 ,而不是6 By removing an element and moving i forward, we effectively skipped checking an element. 通过删除元素并将 i向前移动,我们有效地跳过了对元素的检查。

Your code is doing a similar operation, and suffers from a similar problem. 您的代码正在执行类似的操作,并且遇到类似的问题。 You can both simplify your code and correct the issue by using built-in functions, such as removeAll : 您可以使用内置函数(例如removeAll来简化代码并解决问题:

playerOneInputArray.removeAll(playerTwoInputArray);

This will remove the duplicates from playerOneInputArray , but if you want to do this to playerTwoInputArray you'll have to change it a little bit, as once you've removed the duplicates from the first arrayList you can't remember which duplicates you removed. 这会从playerOneInputArray删除重复playerOneInputArray ,但是如果要对playerTwoInputArray执行此操作,则必须playerTwoInputArray更改,因为一旦从第一个arrayList中删除了重复项,您将不记得删除了哪些重复项。 If that is the case, consider: 如果是这种情况,请考虑:

ArrayList<Character> duplicates = new ArrayList<Character>(playerOneInputArray);
duplicates.retainAll(playerTwoInputArray);
playerOneInputArray.removeAll(duplicates);
playerTwoInputArray.removeAll(duplicates);

Your problem is that you modify your playerOneInputArray in loop. 您的问题是您在循环中修改了playerOneInputArray Your code doesnt work as you expect because when you do remove() , then playerOneInputArray.size() changes. 您的代码playerOneInputArray.size()按预期工作,因为当您执行remove()playerOneInputArray.size()发生变化。 So, i recommend to use removeAll(playerTwoInputArray) instead of remove in loop. 因此,我建议使用removeAll(playerTwoInputArray)而不是在循环中删除。

An observation about your code: 关于您的代码的观察:

You will encounter index out of bounds exception when the first array is longer than the second. 当第一个数组长于第二个数组时,您将遇到索引超出范围异常。

Also you will not check all elements between the arrays when the two arrays have different sizes because you are only using the size of the first array for the loop. 同样,当两个数组的大小不同时,您也不会检查数组之间的所有元素,因为您仅将第一个数组的大小用于循环。 Doing that creates the potential for the first issue I mentioned as well. 这样做也为我提到的第一个问题创造了潜力。

You need to handle these cases in some way as opposed to skipping some data or having an error. 您需要以某种方式处理这些情况,而不是跳过某些数据或出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM