英文:
Print values of ArrayList<ArrayList<Integer>> that are not contained in another ArrayList<ArrayList<~>>
问题
我试图迭代遍历两个名为 list
和 list2
的 ArrayList<ArrayList<Integer>>
,并在删除重复项的同时打印结果。
list
包含:
[[4], [3], [0,2,1]]
而 list2
包含:
[[2,3], [0], [1], [4], []]
我的当前代码如下:
//size=list.size();
Integer temp1;
for(int num=0;num<size;num++){
System.out.print(list.get(num)+" ");
temp1=list.get(num).get(0);
System.out.println(list2.get(temp1));
}
对于列表中位置 0 的每个值,我希望检查在 list2
中的位置,并打印内容,而不打印可能也在列表中的值。例如,当前我的输出如下所示,对于第三行,我希望能够仅打印 [2,3] 中的 3,因为值 2 在 [0,2,1] 中。
[4] []
[3] [4]
[0, 2, 1] [2,3]
我不确定如何高效地删除重复值并只打印唯一值。
英文:
I'm attempting to iterate through two ArrayList<ArrayList<Integer>>
named list
and list2
and print the results while removing the duplicates.
list
contains:
[[4], [3], [0,2,1]]
and list2
contains:
[[2,3], [0], [1], [4], []]
My current code looks like:
//size=list.size();
Integer temp1;
for(int num=0;num<size;num++){
System.out.print(list.get(num)+" ");
temp1=list.get(num).get(0);
System.out.println(list2.get(temp1));
}
For each value in list position 0, I want to check the position in list2
and print the contents without printing the value that may be in list as well.
For example, currently my output looks like below and for the third line I want to be able to print just the 3 in [2,3] as the value 2 is in [0,2,1].
[4] []
[3] [4]
[0, 2, 1] [2,3]
I'm unsure as to how to efficiently remove the duplicate values and print just the unique value.
答案1
得分: 1
首先,您将list
中的子列表保存到临时数组中,并打印出您已经完成的部分:
ArrayList<Integer> temp;
temp = list.get(num);
System.out.print(list.get(num) + " ");
接着,取出temp1
,并从list2
中获取子列表,以便进行比较和去除重复项:
temp1 = list.get(num).get(0);
ArrayList<Integer> list2sub = list2.get(temp1);
迭代temp
列表,从list2sub
中移除所有重复项,并将list2sub
打印出来:
for (Integer element : temp) {
list2sub.remove(element);
}
System.out.println(list2sub);
最后,代码应该类似于这样:
int size = list.size();
Integer temp1;
for (int num = 0; num < size; num++) {
ArrayList<Integer> temp;
temp = list.get(num);
System.out.print(list.get(num) + " ");
temp1 = list.get(num).get(0);
ArrayList<Integer> list2sub = list2.get(temp1);
for (Integer element : temp) {
list2sub.remove(element);
}
System.out.println(list2sub);
}
输出结果如下:
[4] []
[3] [4]
[0, 2, 1] [3]
如果您不允许更改子列表中的值,即不执行.remove
操作,您只需将结果放入另一个temp
数组列表中,并将其打印出来,而不是修改list2sub
。
英文:
First you save sublist from list
in temporal array and print it out what you already did:
ArrayList<Integer> temp;
temp = list.get(num);
System.out.print(list.get(num)+" ");
Take temp1
and get sublist from list2
to be able to compare and remove duplicates:
temp1=list.get(num).get(0);
ArrayList<Integer> list2sub = list2.get(temp1);
Iterate over temp
list and remove all duplicates from list2sub
and print list2sub
out:
for (Integer element : temp) {
list2sub.remove(element);
}
System.out.println(list2sub);
Finally it should look something like this:
int size=list.size();
Integer temp1;
for(int num=0;num<size;num++){
ArrayList<Integer> temp;
temp = list.get(num);
System.out.print(list.get(num)+" ");
temp1=list.get(num).get(0);
ArrayList<Integer> list2sub = list2.get(temp1);
for (Integer element : temp) {
list2sub.remove(element);
}
System.out.println(list2sub);
}
And output is this:
[4] []
[3] [4]
[0, 2, 1] [3]
If you are not allowed to change values in sublists i.e. doing .remove
then you would just put it in another temp
array list and print it out instead of modifying list2sub
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论