Rewrite a Java array based on the values ​​of another array

I worked in a small Java project, but cannot figure out how to overwrite the elements of an array based on the values ​​in another array.

Basically I have two arrays: repeated[] = {1,4,0,0,0,3,0,0}and hand[] = {1,2,2,2,2,6,6,6}, and I use repeated[]to count the number of times a number appears on hand[], and if it is between 3 and 7, it should overwrite the corresponding element in hand[]with zero, but I continue to get this output {1,0,0,2,2,6,0,6}when it must give me {1,0,0,0,0,0,0,0}. What am I doing wrong?

public static void main(String[] args) {
    int repeated[] = {1,4,0,0,0,3,0,0};
    int hand[] = {1,2,2,2,2,6,6,6};
    for(int z=0;z<repeated.length;z++){
        if(repeated[z]>=3 && repeated[z]<8){
            for(int f:hand){
                if(hand[f]==(z+1)){
                    hand[f]=0;
                } } } }
    for(int e:hand){
        System.out.print(e+",");
    }
    } 
+4
source share
1 answer

-, repeated ( Java ). , >= 3 ( 6 3 ). Arrays.toString(int[]) . - ,

public static void main(String[] args) {
    int repeated[] = { 1, 4, 0, 0, 0, 3, 0, 0 };
    int hand[] = { 1, 2, 2, 2, 2, 6, 6, 6 };
    for (int z = 0; z < repeated.length; z++) {
        if (repeated[hand[z] - 1] >= 3) {
            hand[z] = 0;
        }
    }
    System.out.println(Arrays.toString(hand));
}

[1, 0, 0, 0, 0, 0, 0, 0]
+3

Source: https://habr.com/ru/post/1650982/


All Articles