Separating even and odd elements into two arrays involves iterating through the original array, checking each element’s parity using the modulus operator (%
), and adding it to the respective array. In this article, we will explore two practical approaches to separate even and odd numbers from an array into two distinct arrays using Java. This task is fundamental for learning array manipulation and understanding how to work with conditional logic effectively.
Approach 1: Using Stream API for Simplicity
In the first method, we utilize Java’s Stream API to filter and collect even and odd numbers into separate lists. These lists are then converted into arrays for the final output. This approach is concise and modern, ideal for developers familiar with Java 8 and later versions.
package com.javacodepoint.array;
import java.util.ArrayList;
public class SeparateEvenOddExample1 {
public static void main(String[] args) {
int[] arr = { 10, 15, 20, 25, 30, 35, 40 };
ArrayList<Integer> evenList = new ArrayList<>();
ArrayList<Integer> oddList = new ArrayList<>();
// Separate even and odd numbers
for (int num : arr) {
if (num % 2 == 0) {
evenList.add(num);
} else {
oddList.add(num);
}
}
// Convert lists to arrays
int[] evenArray = evenList.stream().mapToInt(i -> i).toArray();
int[] oddArray = oddList.stream().mapToInt(i -> i).toArray();
// Print results
System.out.println("Even elements: ");
for (int num : evenArray) {
System.out.print(num + " ");
}
System.out.println("\nOdd elements: ");
for (int num : oddArray) {
System.out.print(num + " ");
}
}
}
OUTPUT:
Even elements:
10 20 30 40
Odd elements:
15 25 35
Code Explanation:
- We initialize an array with a mix of even and odd numbers.
- We create two
ArrayList
objects to store even and odd numbers separately. - We loop through the original array and add it to the respective list based on whether the number is even or odd.
- We then convert the
ArrayList
objects back into arrays. - Finally, we print both even and odd arrays.
Approach 2: Without Stream API for Manual Control
For a more traditional solution, the second approach avoids the Stream API and instead uses loops to count and populate separate arrays for even and odd numbers. This method is great for understanding the fundamentals of array processing and manual allocation.
package com.javacodepoint.array;
public class SeparateEvenOddExample2 {
public static void main(String[] args) {
int[] arr = { 10, 15, 20, 25, 30, 35, 40 };
// Count even and odd numbers
int evenCount = 0, oddCount = 0;
for (int num : arr) {
if (num % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
}
// Create arrays for even and odd numbers
int[] evenArray = new int[evenCount];
int[] oddArray = new int[oddCount];
// Populate the arrays
int evenIndex = 0, oddIndex = 0;
for (int num : arr) {
if (num % 2 == 0) {
evenArray[evenIndex++] = num;
} else {
oddArray[oddIndex++] = num;
}
}
// Print results
System.out.println("Even elements:");
for (int num : evenArray) {
System.out.print(num + " ");
}
System.out.println("\nOdd elements:");
for (int num : oddArray) {
System.out.print(num + " ");
}
}
}
OUTPUT:
Even elements:
10 20 30 40
Odd elements:
15 25 35
Code Explanation:
- We first count the number of even and odd elements to determine the sizes of the
evenArray
andoddArray
. - We then create these arrays with the appropriate sizes.
- We loop through the original array again to populate the
evenArray
andoddArray
. - Finally, we print both even and odd arrays.
Conclusion
Both approaches offer effective ways to separate even and odd elements from an array in Java. The choice between them depends on your specific use case—whether you need dynamic resizing or prefer a more memory-efficient solution.
Read also: Remove duplicate elements in an Array. | Merge Two Arrays. | Sum of Two Arrays.