合并两个的ArrayList到一个新的ArrayList,没有重复,为了在Java中 [英] Merging two arrayLists into a new arrayList, with no duplicates and in order, in Java
问题描述
我想结合两的ArrayList,产生包含了两个组合的ArrayList所有的数字一个新的ArrayList,但没有任何重复的元素,他们应该是为了。我想出了这个code以下。我通过它运行,并且对我来说很有意义,但是我不知道如果我可以用<或>比较获得(I)的中的ArrayList。我加入的ARRAY1所有元素融入plusArray。然后,我将通过plusArray和比较它ARRAY2,看是否有数组2的元素的内部plusArray存在。如果他们这样做,我什么都不做,但如果他们不那么我想它添加在它的正确位置。也许我的嵌套的for循环被错误地使用?注:的ArrayList是由用户按升序排列psorted $ P $
的ArrayList<整数GT; plusArray =新的ArrayList<整数GT;();
的for(int i = 0; I< array1.size();我++){
plusArray.add(array1.get(I));
}的for(int i = 0; I< plusArray.size();我++){
对于(INT J = 0; J< array2.size(); J ++){ 如果(array2.get(J)所述; plusArray.get(ⅰ)){
plusArray.add(I,array2.get(J));
}
否则,如果(plusArray.get(I).equals(array2.get(J))){
;
}
否则如果(array2.get(J)> plusArray.get(ⅰ)){
plusArray.add(I,array2.get(J));
}}
更新:我不明白下面的异常了。相反,它似乎在程序运行,直到永远。我改变了在哪里添加在&lt元素的位置;和>条件。
///
这是当我的数组列表是我得到异常:
INTSET 1:{1 2}
INTSET 2:{1 3 4}
异常线程mainjava.lang.OutOfMemoryError:Java堆空间
在java.util.Arrays.copyOf(来源不明)
在java.util.Arrays.copyOf(来源不明)
在java.util.ArrayList.grow(来源不明)
在java.util.ArrayList.ensureCapacityInternal(来源不明)
在java.util.ArrayList.add(来源不明)
在IntSet.plus(IntSet.java:92)
在IntSetDriver.main(IntSetDriver.java:61)
相反,code你写的,你可以使用 ArrayList.addAll()
合并列表, Collections.sort()
来排序并 O(N)
来遍历结果ArrayList的去除重复。
I am trying to "combine" two arrayLists, producing a new arrayList that contains all the numbers in the two combined arrayLists, but without any duplicate elements and they should be in order. I came up with this code below. I run through it and it makes sense to me, but Im not sure if I can be using < or > to compare get(i)'s in arrayLists. I am adding all the elements in array1 into the plusArray. Then I am going through the plusArray and comparing it to array2 to see if any of array2's elements exist inside plusArray. If they do I am doing nothing, but if they dont then I am trying to add it in its correct position. Perhaps my nested for loops being used incorrectly? Note: The ArrayLists are presorted by the user in increasing order.
ArrayList<Integer> plusArray = new ArrayList<Integer>();
for(int i = 0; i < array1.size(); i++){
plusArray.add(array1.get(i));
}
for(int i = 0; i < plusArray.size(); i++){
for(int j = 0; j < array2.size(); j++){
if(array2.get(j) < plusArray.get(i)){
plusArray.add(i,array2.get(j));
}
else if(plusArray.get(i).equals(array2.get(j))){
;
}
else if(array2.get(j) > plusArray.get(i)){
plusArray.add(i, array2.get(j));
}
}
UPDATE: I dont get the exception below anymore. Instead it seems the program runs forever. I changed the location of where to add the elements in the < and > conditions. /// Here is the exception that I get when my array lists are: IntSet 1: { 1 2 } IntSet 2: { 1 3 4 }
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.util.Arrays.copyOf(Unknown Source)
at java.util.ArrayList.grow(Unknown Source)
at java.util.ArrayList.ensureCapacityInternal(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at IntSet.plus(IntSet.java:92)
at IntSetDriver.main(IntSetDriver.java:61)
Instead of the code you wrote, you may use ArrayList.addAll()
to merge the lists, Collections.sort()
to sort it and O(n)
to traverse of the resulting ArrayList to remove duplicates.
这篇关于合并两个的ArrayList到一个新的ArrayList,没有重复,为了在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!