简体   繁体   English

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

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

i have two arrays filled with random int values.What i want to do is put the elements that are in one of the arrays but not in the other on a third array.How can i do this?我有两个填充随机 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);
}

can anybody help me to continue the code jus to achieve whatever i said,thank you very much what i want to achieve is:任何人都可以帮助我继续代码以实现我所说的一切,非常感谢您我想要实现的是:

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]

The most straightforward way would be to use collections to do this.最直接的方法是使用集合来做到这一点。 Create a list from the first array and a set from the second.从第一个数组创建一个列表,从第二个数组创建一个集合。 Then remove all elements of the second collection from the first one.然后从第一个集合中删除第二个集合的所有元素。 Finally, produce an array.最后,生成一个数组。 Something along the lines:沿线的东西:

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);

You could try a nested for loop?您可以尝试嵌套 for 循环吗? Not the most efficient way but simple enough for your needs...不是最有效的方法,但足够简单满足您的需求......

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...
    }
}

The best thing to do here is step back and think about what you're trying to solve.在这里做的最好的事情是退后一步,想想你要解决的问题。 You have two arrays each with random numbers in, lets say they are something like:你有两个数组,每个数组都有随机数,假设它们是这样的:

Array A :阵列A

1,3,4,5,6 1、3、4、5、6

Array B :数组B

4,9,4,5,6 4,9,4,5,6

So we need to find the the values within array A which are not in array B .所以我们需要找到数组A不在数组B 中的值 How would you work this out on paper?你会如何在纸上解决这个问题? You would start with the first number of array A and scan through each number within array B to check whether its in there, if it is we stop looking, else we continue to the last number.您将从数组A的第一个数字开始并扫描数组B 中的每个数字以检查它是否在那里,如果是我们停止查找,否则我们继续到最后一个数字。 So for the example above we would find:所以对于上面的例子,我们会发现:

Array Result :数组结果

1,3 1,3

So lets translate this into some code:所以让我们把它翻译成一些代码:

// 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]);
    }
}

Try with two for loops.尝试使用两个 for 循环。 First loop through the first array and consider each current element, then loop through the second array to check if the current number from array1 is in array2 .首先遍历第一个数组并考虑每个当前元素,然后遍历第二个数组以检查array1current数字是否在array2 If it is not present in array2 , then add it to array3 .如果它不存在于array2 ,则将其添加到array3 Lastly I added a piece of code to trim array3 to size.最后我添加了一段代码来修剪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.

相关问题 如何删除数组中的指定字符并将元素复制到另一个数组 - how to delete specified character in array and copy the elements to another array 如何将数组中的元素复制到另一个大小不同的数组中? - How can I copy the elements on an array into another array with a different size? 如何将数组列表的元素从另一个类复制到另一个类 - How to copy elements of array list from another class to another class 如何在Java中仅将一个数组的某些元素复制到另一个 - How to copy only certain elements of one Array to another in Java 如何在循环中将数组元素从一个复制到另一个? - How can i copy array elements from one to another in loop? 如何比较数组元素并将相应的元素添加到第三个数组? - How to compare array elements and add corresponding elements to a third array? 元素的总和到第三个数组 - Array sum of elements to third array 如何以相反的顺序将一个数组中最左边的 n 个元素复制到另一个数组中最右边的 n 个位置 - How to copy the leftmost n elements in one array into the rightmost n position in another array in reverse order 如何将数组操作“复制”到另一个数组? - How to “copy” an array operation to another array? 如何将这 3 张卡片复制到第三个数组中 - How do I copy those 3 cards in a third array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM