从 ArrayList#1 中删除出现在另一个 ArrayList#2 中且在 ArrayList#1 中不唯一的行 [英] Delete rows from ArrayList#1 that appear in another ArrayList#2 and are non-unique in ArrayList#1

查看:14
本文介绍了从 ArrayList#1 中删除出现在另一个 ArrayList#2 中且在 ArrayList#1 中不唯一的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个数组列表:

ArrayListarr1 = new ArrayList();ArrayListarr2 = new ArrayList();arr1.add(新整数[]{1,2,3});arr1.add(新整数[]{1,2,2});arr1.add(新整数[]{1,2,3});arr1.add(新整数[]{1,1,1});arr1.add(新整数[]{1,1,1});arr2.add(新整数[]{1,2,3});arr2.add(新整数[]{1,2,2});

如何从arr1中删除出现在arr2中并且在arr1中不唯一的行?例如.在这个例子中,我只需要删除 {1,2,3},因为它在 arr1 中出现了不止一次.我想到的唯一解决方案是使用 4 个 FOR 循环,但这似乎是非常低效的解决方案.

编辑#1结果 arr1: {1,2,3},{1,2,2},{1,1,1},{1,1,1}

解决方案

如果您可以使用 List 而不是数组,那么您可以执行以下操作:

List>arr1 = new ArrayList>();列表<列表<整数>>arr2 = new ArrayList>();arr1.add(Arrays.asList(new Integer[]{1, 2, 3}));arr1.add(Arrays.asList(new Integer[]{1, 2, 3}));arr1.add(Arrays.asList(new Integer[]{1, 2, 2}));arr1.add(Arrays.asList(new Integer[]{1, 2, 3}));arr1.add(Arrays.asList(new Integer[]{1, 1, 1}));arr1.add(Arrays.asList(new Integer[]{1, 1, 1}));arr2.add(Arrays.asList(new Integer[]{1, 2, 3}));arr2.add(Arrays.asList(new Integer[]{1, 2, 2}));System.out.println(arr1);System.out.println(arr2);设置<列表<整数>>set1 = new HashSet>();迭代器<列表<整数>>它 = arr1.iterator();while(it.hasNext()) {列表<整数>curr = it.next();if(!set1.add(curr) && arr2.contains(curr)) {it.remove();}}System.out.println(arr1);

输出:

<前>[[1, 2, 3], [1, 2, 3], [1, 2, 2], [1, 2, 3], [1, 1, 1], [1, 1, 1]][[1, 2, 3], [1, 2, 2]][[1, 2, 3], [1, 2, 2], [1, 1, 1], [1, 1, 1]]

There are two arraylists:

ArrayList<Integer[]> arr1 = new ArrayList<Integer[]>();
ArrayList<Integer[]> arr2 = new ArrayList<Integer[]>();
arr1.add(new Integer[]{1,2,3});
arr1.add(new Integer[]{1,2,2});
arr1.add(new Integer[]{1,2,3});
arr1.add(new Integer[]{1,1,1});
arr1.add(new Integer[]{1,1,1});

arr2.add(new Integer[]{1,2,3});
arr2.add(new Integer[]{1,2,2});

How to delete rows from arr1 that appear in arr2 and that are non-unique in arr1? E.g. in this example I would need to delete only {1,2,3}, because it appears more than once in arr1. The only solution that comes to my mind is to use 4 FOR loops, but it seems to be very inefficient solution.

Edit#1 Resulting arr1: {1,2,3},{1,2,2},{1,1,1},{1,1,1}

解决方案

In case if you can use a List instead of array then you could do something as follows:

List<List<Integer>> arr1 = new ArrayList<List<Integer>>();        
List<List<Integer>> arr2 = new ArrayList<List<Integer>>();

arr1.add(Arrays.asList(new Integer[]{1, 2, 3}));
arr1.add(Arrays.asList(new Integer[]{1, 2, 3}));
arr1.add(Arrays.asList(new Integer[]{1, 2, 2}));
arr1.add(Arrays.asList(new Integer[]{1, 2, 3}));
arr1.add(Arrays.asList(new Integer[]{1, 1, 1}));
arr1.add(Arrays.asList(new Integer[]{1, 1, 1}));

arr2.add(Arrays.asList(new Integer[]{1, 2, 3}));
arr2.add(Arrays.asList(new Integer[]{1, 2, 2}));

System.out.println(arr1);
System.out.println(arr2);

Set<List<Integer>> set1 = new HashSet<List<Integer>>();        
Iterator<List<Integer>> it = arr1.iterator(); 

while(it.hasNext()) {
    List<Integer> curr = it.next();
    if(!set1.add(curr) && arr2.contains(curr)) {
        it.remove();
    }
}

System.out.println(arr1);

Output:

[[1, 2, 3], [1, 2, 3], [1, 2, 2], [1, 2, 3], [1, 1, 1], [1, 1, 1]]
[[1, 2, 3], [1, 2, 2]]
[[1, 2, 3], [1, 2, 2], [1, 1, 1], [1, 1, 1]]

这篇关于从 ArrayList#1 中删除出现在另一个 ArrayList#2 中且在 ArrayList#1 中不唯一的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆