简体   繁体   English

Java数组删除会带来意想不到的结果吗?

[英]Java array removal gives unexpected results?

The following code is part of a program which takes the values of array ar except for variable z, and copies it onto a different array called ar2. 以下代码是程序的一部分,该程序采用数组ar的值(变量z除外)并将其复制到另一个名为ar2的数组中。 The result should be all the numbers except negative two (19, 1, 17, 17), but currently the result is 19 1 17 17 -2 19 1 17 17 -2 19 1 17 17 -2 19 1 17 17 -2. 结果应为除负数2(19、1、17、17)以外的所有数字,但当前结果为19 1 17 17 -2 19 1 17 17 -2 19 1 17 17 -2 19 1 17 17 -2。

public class Second_tiny {

    public static void main(String[] args) {
        int[] ar = { 19, 1, 17, 17, -2 };
        int z = ar[0];

        for (int i = 0; i < (ar.length); i++) {
            if (z > ar[i]) {
                z = ar[i];

            }
        }
        // second pass
        int[] ar2 = new int[ar.length];
        int zero = 0;
        for (int x = 0; x < (ar.length); x++) {
            if (ar[x] == z) {
                continue; // If it is equal to z go back to the loop again

            }
            ar2[zero++] = ar[x];

            for (int i = 0; i < ar.length; i++) {
                System.out.println(ar[i]);
            }
            /*
             * //2nd pass copy all items except smallest one to 2nd array int[] ar2= new int[ar.length-1]; int curIndex = 0; for (i=0; i<ar.length; i++) { if (ar[i]==z) continue; ar2[curIndex++] =
             * ar[i]; }
             */

        }

    }
}    

Java 8 way: Java 8方式:

        int[] ar = { 19, 1, 17, 17, -2 };
        int min = Arrays.stream(ar).min().getAsInt();
        int[] ar2 = Arrays.stream(ar).filter(s -> s!=min).toArray();
        System.out.println(Arrays.toString(ar2));

You're printing out your original array 4 times in this block: 您在此块中将原始阵列打印4次:

for (int i = 0; i < ar.length; i++) {
    System.out.println(ar[i]);
}

That block should be outside of your loop, and should reference ar2 instead of ar . 该块应该在循环之外,并且应该引用ar2而不是ar

for (int x = 0; x < (ar.length); x++) {
    if (ar[x] == z) {
        continue; // If it is equal to z go back to the loop again
    }

    ar2[zero++] = ar[x];
}

for (int i = 0; i < ar2.length; i++) {
    System.out.println(ar2[i]);
}

This will give you the following result: 这将为您提供以下结果:

19
1
17
17
0

The last 0 appears because 0 is the default value for int s. 最后一个0出现是因为0int的默认值。 Your ar2 array is 5 elements long, and for the last element the default value is never replaced. 您的ar2 5个元素,对于最后一个元素,永远不会替换默认值。

You could use Math.min(int, int) to determine your lowest value. 您可以使用Math.min(int, int)来确定最小值。 Your second array should be one element smaller. 您的第二个数组应该小一个元素。 I suggest guarding against removing more than one value. 我建议您避免删除多个值。 And you could use Arrays.toString(int[]) to print the second array . 您可以使用Arrays.toString(int[])来打印第二个array Something like, 就像是,

int[] ar = { 19, 1, 17, 17, -2 };
int z = ar[0];
for (int i = 1; i < ar.length; i++) {
    z = Math.min(z, ar[i]);
}
// second pass
int[] ar2 = new int[ar.length - 1];
boolean first = true;
for (int x = 0; x < ar.length; x++) {
    if (ar[x] == z && first) {
        first = false;
        continue; // If it is equal to z go back to the loop again
    }
    int y = x - (!first ? 1 : 0);
    ar2[y] = ar[x];
}
System.out.println(Arrays.toString(ar2));

Output is 输出是

[19, 1, 17, 17]

在此处输入图片说明 you code is right mistakenly you have given one extra for loop.i have done comment on that for loop.use the following code u will get the desired output. 您的代码错误是对的,您给了一个额外的for循环。我对for循环做了注释。使用以下代码,您将获得所需的输出。

public class HelloWorld { 公共类HelloWorld {

    public static void main(String[] args) {
        int[] ar = { 19, 1, 17, 17, -2 };
        int z = ar[0];

        for (int i = 0; i < (ar.length); i++) {
            if (z > ar[i]) {
                z = ar[i];

            }
        }
        // second pass
        int[] ar2 = new int[ar.length];
        int zero = 0,i=0;

        for (int x = 0; x < (ar.length); x++) {
            if (ar[x] == z) {
                continue; // If it is equal to z go back to the loop again

            }
            ar2[zero++] = ar[x];

          //  for (int i = 0; i < ar.length; i++) {
                System.out.println(ar[i]);
                i++;
          //  }
            /*
             * //2nd pass copy all items except smallest one to 2nd array int[] ar2= new int[ar.length-1]; int curIndex = 0; for (i=0; i<ar.length; i++) { if (ar[i]==z) continue; ar2[curIndex++] =
             * ar[i]; }
             */

        }

    }
}    

output is: 输出为:

19

1

17

17

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

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