How can I create a copy of an array with only the even numbers?

时间:2016-04-04 18:05:15

标签: java arrays

I have to create a method that copies and prints the even numbers in an array by creating a new array. I already created an array called myNumbers. I think the problem is that myEvenNumbers[z] will not store an even number at myEvenNumbers[0] but rather it would store whatever the first even number is at the first index of myNumbers. Any help would be much appreciated!

This is my code so far.

public static void copyAndPrintEvenNumbers() {
    System.out.println("All the even numbers are: ");
    int count = 0;

    for (int i =0; i < myNumbers.length; i++) {
        if (myNumbers[i] % 2 == 0) {
            count++;
        }
        int[] myEvenNumbers = new int[count];
        for (int z = 0; z < myNumbers.length; z++) {
            if (myNumbers[z] % 2 ==0) {
                myEvenNumbers[z] = myNumbers[z];
                System.out.println(myEvenNumbers[z]);
            }
        }

    }

}

2 个答案:

答案 0 :(得分:1)

You must declare a second index for the myEvenNumbers array. When z exceeds the total number of elements of the myEvenNumbers array, you'll get a IndexOutOfBounds. Also like Connor said, the two loops should not be nested. Else you wil discard the previous array instance (myEvenNumbers)and will result into a memory leak.

public static void copyAndPrintEvenNumbers() {

    System.out.println("All the even numbers are: ");
    int count = 0;

    for (int i =0; i < myNumbers.length; i++) {
        if (myNumbers[i] % 2 == 0) {
            count++;
        }
    }

    int[] myEvenNumbers = new int[count];
    int myEvenIndex = 0;
    for (int z = 0; z < myNumbers.length; z++) {
        if (myNumbers[z] % 2 ==0) {
            myEvenNumbers[myEvenIndex] = myNumbers[z];
            System.out.println(myEvenNumbers[myEvenIndex]);
            myEvenIndex++;
        }
    }

}

Personally I would select the same size for the even numbers array (a little overhead for memory) to eliminate the first loop, because the count of even numbers cannot exceed the count of the original array.

public static void copyAndPrintEvenNumbers() {

    System.out.println("All the even numbers are: ");
    int count = 0;

    int[] myEvenNumbers = new int[myNumbers.length];
    int count = 0;
    for (int z = 0; z < myNumbers.length; z++) {
        if (myNumbers[z] % 2 ==0) {
            myEvenNumbers[count] = myNumbers[z];
            System.out.println(myEvenNumbers[count]);
            count++;
        }
    }
    System.out.println("There are " + count + " even numbers found");
}

答案 1 :(得分:0)

There is no need for your second for loop to be within the first. Try this:

public static void copyAndPrintEvenNumbers() {
    System.out.println("All the even numbers are: ");
    int count = 0;

    for (int i =0; i < myNumbers.length; i++) {
        if (myNumbers[i] % 2 == 0) {
            count++;
        }
    }

    int[] myEvenNumbers = new int[count];
    int index = 0;
    for(int i = 0; i < myNumbers.length; i++) {
        if (myNumbers[i] % 2 ==0) {
            myEvenNumbers[index] = myNumbers[i];
            index++;
        }
    }
    System.out.println(myEvenNumbers);
}