簡體   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