繁体   English   中英

如何复制数组中的元素但不在第三个数组中的另一个数组中

[英]how to copy elements that are in an array but not in another on a third array

我有两个填充随机 int 值的数组。我想要做的是将位于其中一个数组中但不在另一个数组中的元素放在第三个数组中。我该怎么做?

int[] array1 = new int[10];
int[] array2 = new int[10];

for(int j=0;j<array2.length;j++){
    int alea = rant.nextInt(10);
    valorpos = alea;
    array1[j] = rant.nextInt(10);
    array2[j] = rant.nextInt(10);
}

任何人都可以帮助我继续代码以实现我所说的一切,非常感谢您我想要实现的是:

array1:[5,7,9,2,9,8,3,2,4,8];
Array 2: [6,2,5,8,3,0,5,9,2,0];
resultat:[7,4,6,0,0]

最直接的方法是使用集合来做到这一点。 从第一个数组创建一个列表,从第二个数组创建一个集合。 然后从第一个集合中删除第二个集合的所有元素。 最后,生成一个数组。 沿线的东西:

List<Integer> list1 = Arrays.stream(array1).boxed().collect(Collectors.toList());
Set<Integer> set2 = Arrays.stream(array2).boxed().collect(Collectors.toSet());
list1.removeAll(set2);
Integer[] array3 = new Integer[list1.size()];
list1.toArray(array3);

您可以尝试嵌套 for 循环吗? 不是最有效的方法,但足够简单满足您的需求......

boolean contain;

//loop through the first array
for(int number1 : array1){
    //set contain to false
    contain = false;

    //loop through the second array
    for(int number2 : array2){

        //if number1 in first array equals number2 in second array
        if(number1 == number2){
            //set contain to true and break the loop
            contain = true;
            break;
        }
    }
    //if contain is still false
    if(!contain){
        //add number1 to third array...
    }
}

在这里做的最好的事情是退后一步,想想你要解决的问题。 你有两个数组,每个数组都有随机数,假设它们是这样的:

阵列A

1、3、4、5、6

数组B

4,9,4,5,6

所以我们需要找到数组A不在数组B 中的值 你会如何在纸上解决这个问题? 您将从数组A的第一个数字开始并扫描数组B 中的每个数字以检查它是否在那里,如果是我们停止查找,否则我们继续到最后一个数字。 所以对于上面的例子,我们会发现:

数组结果

1,3

所以让我们把它翻译成一些代码:

// a list which will be used to put the unique values in
List<Integer> result = new ArrayList<>();

// loop through each number in the first array
for(int i=0; i< arrayOne.length; i++){

    boolean isInOtherArray = false;
    // check this number against each number in the second array
    for(int j=0; j<arrayTwo.length; j++){

        // if the number matches, then we know its not unique 
        // so we stop the search
        if(arrayOne[i] == arrayTwo[j]){
            isInOtherArray=true;
            break;
        }
    }
    // if its not in the other array we know its unique so it goes 
    // in our result list
    if(!isInOtherArray){
        result.add(arrayOne[i]);
    }
}

尝试使用两个 for 循环。 首先遍历第一个数组并考虑每个当前元素,然后遍历第二个数组以检查array1current数字是否在array2 如果它不存在于array2 ,则将其添加到array3 最后我添加了一段代码来修剪array3的大小。

    int[] array1=new int[10];
    int[] array2=new int[10];
    int[] array3=new int[10];
    // populate array1 and array2 ....

    boolean isInArray2;
    int current;
    int counter = 0;

    for (int i = 0; i < array1.length; i++) {
        current = array1[i];
        isInArray2= false;
        for (int j = 0; j < array2.length; j++) {
            if(array2[j] == current) {
                isInArray2= true;
                break;
            }
        }

        if(!isInArray2) {
            // not in array2 -> must add in array3
            array3[counter] = current;
            counter++;
        }
    }

    // Trim array3 to avoid null values:
    int[] tmp = array3;
    array3=new int[counter];
    for (int i = 0; i < array3.length; i++) {
        array3[i] =tmp[i];
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM