使用Java查找两个数组之间的非重复项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19401618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:51:11  来源:igfitidea点击:

Find non-duplicate items between two arrays with Java

javaarrayshashmapfind

提问by wkg86

I have the following task: There are 2 one dimensional arrays of integers between -20000000 and 20000000. Some of the numbers that are contained in the first array are also contained in the second array. I have to find all the numbers that are contained in the first array but are not contained in the second array. i have to use Java as a language

我有以下任务: -20000000 和 20000000 之间有 2 个一维整数数组。第一个数组中包含的一些数字也包含在第二个数组中。我必须找到包含在第一个数组中但不包含在第二个数组中的所有数字。我必须使用 Java 作为一种语言

Here are the arrays

这是数组

[1, 652 ,5, 15, 385, 4 , 55, 666, 13]

[1、652、5、15、385、4、55、666、13]

[2, 4658, 9, 55, -588, 10, 1083, 17]

[2, 4658, 9, 55, -588, 10, 1083, 17]

Any Ideas how to find it ?

任何想法如何找到它?

EDIT:

编辑:

Here is the final code:

这是最终的代码:

import java.util.ArrayList;
import java.util.List;
public class Values {
public static void main (String[] argv) {

int[] Array1 = new int[] {1,652,5,15,385,4,55,666,13};
int[] Array2 = new int[] {2, 4658, 9, 55, -588, 10, 1083, 17};
int calculateResult = 0;
boolean contains = false;
int mod = 123456789; 
int modSum = 0;

List<Integer> results = new ArrayList<Integer>();
    for(int i=0; i<Array1.length; i++) {
        for(int j=0; j<Array2.length; j++) {
            if(Array1[i]==Array2[j]) {
                contains = true;
                break;
            }
        }
        if(!contains) {
            results.add(Array1[i]);
        }
        else {
            contains = false;
        }
    }
    // calculate the result
    for (int i : results) {
        calculateResult  += i;
    }
    // Print Results
    System.out.println(results);
    System.out.println(calculateResult);
}}

Now I'm trying to load Arrays from .csv file. Any ideas ?

现在我正在尝试从 .csv 文件加载数组。有任何想法吗 ?

采纳答案by kai

This is a possible solution:

这是一个可能的解决方案:

    int[] Array1 = new int[] {1,652,5,15,385,4,55,666,13};
    int[] Array2 = new int[] {2,4658,9,55,-588,10,1083,17};
    boolean contains = false;
    List<Integer> results = new ArrayList<Integer>();


    for(int i=0; i<Array1.length; i++) {
        for(int j=0; j<Array2.length; j++) {
            if(Array1[i]==Array2[j]) {
                contains = true;
                break;
            }
        }
        if(!contains) {
            results.add(Array1[i]);
        }
        else{
            contains = false;
        }
    }

    System.out.println(results);

output:

输出:

[1, 652, 5, 15, 385, 4, 666, 13]

i hope this is what are you looking for.

我希望这就是你要找的。

回答by popfalushi

回答by commit

I don't understand why this question have negative votes, it is actually very interesting.

我不明白为什么这个问题有反对票,实际上很有趣。

Look below which may work for you, you only need to take list instead of array

看看下面这可能对你有用,你只需要取列表而不是数组

        List<Integer> l1 = new ArrayList<Integer>();
        List<Integer> l2 = new ArrayList<Integer>();

        l1.add(1);
        l1.add(3);
        l1.add(5);
        l1.add(7);
        l1.add(8);

        l2.add(2);
        l2.add(3);
        l2.add(4);
        l2.add(7);

        l2.retainAll(l1);  //Now l2 have only common elements of both list this is an optional this will work well when there are thousands of element otherwise only do remove all
        l1.removeAll(l2);  //Here magic happens this will remove common element from l1 so l1 will have only elements what are not in l2

        for(Integer v: l1){
            System.out.println(v);
        }

Output:

输出:

1
5
8

回答by Paolo

Take the first array, and, for each element of it, if this isn't an element of the second array, then take it as good otherwise discard it.

取第一个数组,对于其中的每个元素,如果这不是第二个数组的元素,则将其视为好,否则将其丢弃。

Now you have just to study how to write it in java language!

现在你只需要学习如何用java语言编写它!

回答by Prabhakaran Ramaswamy

You can achieve like this.

你可以这样实现。

Integer[] array1  = {1, 652 ,5, 15, 385, 4 , 55, 666, 13};
Integer[] array2 ={2, 4658, 9, 55, -588, 10, 1083, 17};
List<Integer> list=  new ArrayList<Integer>(Arrays.asList(array1));
TreeSet<Integer> set = new TreeSet<Integer>(list);
set.removeAll(Arrays.asList(array2));

System.out.println(set);