Java: Separate Even And Odd Numbers In Array
Are you looking to learn how to write a Java program that efficiently separates even and odd numbers from an array? You've come to the right place! This article will guide you through the process, providing a clear explanation of the code and its logic. We'll break down the provided Java code snippet, discuss its functionality, and explore the underlying concepts.
Understanding the Code
The Java code provided aims to take an array of 20 integers as input and rearrange it into a new array where all the even numbers come first, followed by the odd numbers. Let's dive into the code step by step:
import java.util.Scanner;
public class Ex1_SepararParImpar {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] A = new int[20];
int[] B = new int[20];
System.out.println("Digite 20 inteiros:");
for (int i = 0; i < 20; i++) A[i] = sc.nextInt();
int idx = 0;
// pares
for (int i = 0; i < 20; i++) if (A[i] % 2 == 0) B[idx++] = A[i];
// ímpares
for (int i = 0; i < 20; i++) if (A[i] % 2 != 0) B[idx++] = A[i];
System.out.println("Vetor B (pares primeiro, depois ímpares):");
for (int i = 0; i < 20; i++) System.out.print(B[i] + (i<19?" ":"\n"));
sc.close();
}
}
1. Importing the Scanner Class
The code begins by importing the Scanner class, which is essential for reading user input from the console.
import java.util.Scanner;
2. Declaring the Class
Next, we declare a public class named Ex1_SepararParImpar. The public keyword makes this class accessible from other parts of the program.
public class Ex1_SepararParImpar {
3. The main Method
The main method is the entry point of the Java program. It's where the execution begins.
public static void main(String[] args) {
4. Creating a Scanner Object
Inside the main method, a Scanner object named sc is created. This object is used to read input from the standard input stream (System.in), which is typically the console.
Scanner sc = new Scanner(System.in);
5. Declaring Arrays
Two integer arrays, A and B, are declared. Both arrays have a size of 20. Array A will store the initial input, and array B will store the rearranged numbers.
int[] A = new int[20];
int[] B = new int[20];
6. Prompting User Input
The program prompts the user to enter 20 integers using System.out.println(). This message is displayed on the console.
System.out.println("Digite 20 inteiros:");
7. Reading Input into Array A
A for loop iterates 20 times, reading an integer from the user in each iteration using sc.nextInt() and storing it in the corresponding index of array A.
for (int i = 0; i < 20; i++) A[i] = sc.nextInt();
8. Initializing the Index
An integer variable idx is initialized to 0. This variable will serve as an index for array B, keeping track of the next available position to insert an element.
int idx = 0;
9. Separating Even Numbers
This is where the core logic of separating even and odd numbers begins. A for loop iterates through array A. Inside the loop, an if statement checks if the element at the current index (A[i]) is even by using the modulo operator (%). If A[i] % 2 is equal to 0, it means the number is even.
// pares
for (int i = 0; i < 20; i++) if (A[i] % 2 == 0) B[idx++] = A[i];
If the number is even, it's copied to array B at the index idx, and then idx is incremented using the post-increment operator (idx++). This ensures that the next even number will be placed in the next available position in B.
10. Separating Odd Numbers
Following the even number separation, another for loop iterates through array A. This time, the if statement checks if the element at the current index (A[i]) is odd (i.e., A[i] % 2 is not equal to 0).
// ímpares
for (int i = 0; i < 20; i++) if (A[i] % 2 != 0) B[idx++] = A[i];
If the number is odd, it's copied to array B at the index idx, and idx is incremented, similar to the even number handling.
11. Printing the Result
After separating even and odd numbers, the program prints the rearranged array B to the console. It uses System.out.println() to display a message indicating that this is the