Print values of ArrayList<ArrayList<Integer>> that are not contained in another ArrayList<ArrayList<~>>

huangapple go评论79阅读模式
英文:

Print values of ArrayList<ArrayList<Integer>> that are not contained in another ArrayList<ArrayList<~>>

问题

我试图迭代遍历两个名为 listlist2ArrayList<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&lt;ArrayList&lt;Integer&gt;&gt; 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&lt;size;num++){
    System.out.print(list.get(num)+&quot; &quot;);
    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&lt;Integer&gt; temp;
temp = list.get(num);
System.out.print(list.get(num)+&quot; &quot;);

Take temp1 and get sublist from list2 to be able to compare and remove duplicates:

temp1=list.get(num).get(0);
ArrayList&lt;Integer&gt; 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&lt;size;num++){
    ArrayList&lt;Integer&gt; temp;
    temp = list.get(num);
    System.out.print(list.get(num)+&quot; &quot;);
    temp1=list.get(num).get(0);
    ArrayList&lt;Integer&gt; 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.

huangapple
  • 本文由 发表于 2020年10月6日 04:04:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64215459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定