查找两个数组与Java之间的非重复项目 [英] Find non-duplicate items between two arrays with Java

查看:136
本文介绍了查找两个数组与Java之间的非重复项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

下面是阵列

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

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

任何想法如何找到它?

编辑:

下面是最后的code:

 进口的java.util.ArrayList;
进口的java.util.List;
公共类值{
公共静态无效的主要(字符串[] argv的){INT []数组1 = INT新[] {} 1,652,5,15,385,4,55,666,13;
INT [] =数组2 INT新[] {2,4658,9,55,-588,10,1083,17};
INT calculateResult = 0;
布尔包含= FALSE;
INT MOD = 123456789;
INT modSum = 0;清单<整数GT;结果=新的ArrayList<整数GT;();
    的for(int i = 0; I< Array1.length;我++){
        对于(INT J = 0; J< Array2.length; J ++){
            如果(数组1 [I] == ARRAY2 [J]){
                包含= TRUE;
                打破;
            }
        }
        如果(!含){
            results.add(数组1 [I]);
        }
        其他{
            包含= FALSE;
        }
    }
    //计算结果
    对于(INT I:结果){
        calculateResult + =我;
    }
    //打印结果
    的System.out.println(结果);
    的System.out.println(calculateResult);
}}

现在我试图加载从.csv文件阵列。任何想法?


解决方案

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

  INT []数组1 = INT新[] {} 1,652,5,15,385,4,55,666,13;
    INT [] =数组2 INT新[] {2,4658,9,55,-588,10,1083,17};
    布尔包含= FALSE;
    清单<整数GT;结果=新的ArrayList<整数GT;();
    的for(int i = 0; I< Array1.length;我++){
        对于(INT J = 0; J< Array2.length; J ++){
            如果(数组1 [I] == ARRAY2 [J]){
                包含= TRUE;
                打破;
            }
        }
        如果(!含){
            results.add(数组1 [I]);
        }
        其他{
            包含= FALSE;
        }
    }    的System.out.println(结果);

输出:

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

我希望这是你在寻找的。

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

Here are the arrays

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

[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 ?

解决方案

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.

这篇关于查找两个数组与Java之间的非重复项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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