Issue
Is there a way of copying specific numbers from one array to another?
For example:
I have an array {1, 2, 3, 4, 5}
and I want to copy odd and even numbers to separate arrays. So, the result should be
{2, 4}, {1, 3, 5}
Solution
Try this.
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
int[] even = IntStream.of(array).filter(i -> i % 2 == 0).toArray();
int[] odd = IntStream.of(array).filter(i -> i % 2 != 0).toArray();
System.out.println("even = " + Arrays.toString(even));
System.out.println("odd = " + Arrays.toString(odd));
}
output:
even = [2, 4]
odd = [1, 3, 5]
Answered By - sadetaosa