Java数组删除会产生意外结果?

时间:2015-08-10 04:23:02

标签: java arrays

以下代码是程序的一部分,它接受除变量z之外的数组ar的值,并将其复制到名为ar2的不同数组中。结果应该是除负二(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]; }
             */

        }

    }
}    

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

您将在此块中打印出原始阵列4次:

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

该块应该在您的循环之外,并且应该引用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]);
}

这会给你以下结果:

19
1
17
17
0

显示上一个0,因为0int的默认值。您的ar2数组长度为5个元素,对于最后一个元素,默认值永远不会被替换。

答案 2 :(得分:1)

您可以使用Math.min(int, int)来确定最低值。你的第二个数组应该是一个较小的元素。我建议防止删除多个值。您可以使用Arrays.toString(int[])打印第二个数组。像,

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

输出

[19, 1, 17, 17]

答案 3 :(得分:0)

enter image description here你的代码是错误的你已经给了一个额外的循环。我已经对循环做了评论。使用下面的代码你将获得所需的输出。

公共类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]; }
             */

        }

    }
}    

输出是:

19

1

17

17