Skip to main content
added 5 characters in body
Source Link
Joop Eggen
  • 4.7k
  • 15
  • 19

ArrayList would be fine for dynamically sized "arrays."

You could also count the even numbers in advance.

int[] array = {3, 1, 4, 1, 5, 9, 2, 6, 5};
int evenCount = 0;
for (int i = 0; i < array.length; ++i) {
    if (array[i] % 2 == 0) {
        ++evenCount;
    }
}
int[] evenArray = new int[evenCount];
int iEven = 0;
for (int i = 0; i < array.length; ++i) { // ... && iEven < evenCount
    if (array[i] % 2 == 0) {
        evenArray[iEven] = array[i];
        ++iEven;
    }
}
System.out.println("e" + Arrays.toString(evenArray));

As a goodie: the usage of Stream classes: to accomplish bulk operations.

int[] a = IntStream.of(array).filter(n -> n % 2 == 0).toArray();

int[] a = IntStream.of(array) // Turns array into a Stream of ints.
     .filter(n -> n % 2 == 0) // n is declared, and set the next element.
                              // Predicate whether eveneven; filters.
     .toArray();              // Collect the Stream values into an int[].

Streams are a kind of "iteration" and can be used for arrays, collections, files and such. They offer a huge expressiveness. For instance:

int[] a = IntStream.of(array).filter(n -> n % 2 == 0).sorted().toArray();

[2, 4, 6]

ArrayList would be fine for dynamically sized "arrays."

You could also count the even numbers in advance.

int[] array = {3, 1, 4, 1, 5, 9, 2, 6, 5};
int evenCount = 0;
for (int i = 0; i < array.length; ++i) {
    if (array[i] % 2 == 0) {
        ++evenCount;
    }
}
int[] evenArray = new int[evenCount];
int iEven = 0;
for (int i = 0; i < array.length; ++i) {
    if (array[i] % 2 == 0) {
        evenArray[iEven] = array[i];
        ++iEven;
    }
}
System.out.println("e" + Arrays.toString(evenArray));

As a goodie: the usage of Stream classes: to accomplish bulk operations.

int[] a = IntStream.of(array).filter(n -> n % 2 == 0).toArray();

int[] a = IntStream.of(array) // Turns array into a Stream of ints.
     .filter(n -> n % 2 == 0) // n is declared, and set the next element.
                              // Predicate whether even filters.
     .toArray();              // Collect the Stream values into an int[].

Streams are a kind of "iteration" can be used for arrays, collections, files and such. They offer a huge expressiveness. For instance:

int[] a = IntStream.of(array).filter(n -> n % 2 == 0).sorted().toArray();

[2, 4, 6]

ArrayList would be fine for dynamically sized "arrays."

You could also count the even numbers in advance.

int[] array = {3, 1, 4, 1, 5, 9, 2, 6, 5};
int evenCount = 0;
for (int i = 0; i < array.length; ++i) {
    if (array[i] % 2 == 0) {
        ++evenCount;
    }
}
int[] evenArray = new int[evenCount];
int iEven = 0;
for (int i = 0; i < array.length; ++i) { // ... && iEven < evenCount
    if (array[i] % 2 == 0) {
        evenArray[iEven] = array[i];
        ++iEven;
    }
}
System.out.println("e" + Arrays.toString(evenArray));

As a goodie: the usage of Stream classes: to accomplish bulk operations.

int[] a = IntStream.of(array).filter(n -> n % 2 == 0).toArray();

int[] a = IntStream.of(array) // Turns array into a Stream of ints.
     .filter(n -> n % 2 == 0) // n is declared, and set the next element.
                              // Predicate whether even; filters.
     .toArray();              // Collect the Stream values into an int[].

Streams are a kind of "iteration" and can be used for arrays, collections, files and such. They offer a huge expressiveness. For instance:

int[] a = IntStream.of(array).filter(n -> n % 2 == 0).sorted().toArray();

[2, 4, 6]
Source Link
Joop Eggen
  • 4.7k
  • 15
  • 19

ArrayList would be fine for dynamically sized "arrays."

You could also count the even numbers in advance.

int[] array = {3, 1, 4, 1, 5, 9, 2, 6, 5};
int evenCount = 0;
for (int i = 0; i < array.length; ++i) {
    if (array[i] % 2 == 0) {
        ++evenCount;
    }
}
int[] evenArray = new int[evenCount];
int iEven = 0;
for (int i = 0; i < array.length; ++i) {
    if (array[i] % 2 == 0) {
        evenArray[iEven] = array[i];
        ++iEven;
    }
}
System.out.println("e" + Arrays.toString(evenArray));

As a goodie: the usage of Stream classes: to accomplish bulk operations.

int[] a = IntStream.of(array).filter(n -> n % 2 == 0).toArray();

int[] a = IntStream.of(array) // Turns array into a Stream of ints.
     .filter(n -> n % 2 == 0) // n is declared, and set the next element.
                              // Predicate whether even filters.
     .toArray();              // Collect the Stream values into an int[].

Streams are a kind of "iteration" can be used for arrays, collections, files and such. They offer a huge expressiveness. For instance:

int[] a = IntStream.of(array).filter(n -> n % 2 == 0).sorted().toArray();

[2, 4, 6]