In the previous article, we have seen Java Program to Merge Two Sorted Arrays
In this article we are going to see how to remove odd numbers from array java using java programming language. Remove Odd Numbers From List Java, Remove Odd Numbers From Array Javascript, Odd Numbers Java etc.
Java Program to Remove Odd Numbers from Array
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to remove odd numbers from an array.
Method-1: Java Program to Remove Odd Numbers from Array By Static Initialization of Array Elements
Approach:
- Count the number of even elements using a for loop.
- Initialize an array with size of the number of even elements.
- Iterate the original array and insert the even elements into the new array.
Program:
public class Main { public static void main(String[] args) { // initializing array int[] arr = { 1, 2, 3, 4, 5, 6 }; //removeOdd() method called int[] result = removeOdd(arr); System.out.println("After removing the odd elements the array is : "); // print the array for(int i:result) System.out.print(i + " "); } //removeOdd() method to remove odd elements from the array public static int[] removeOdd(int[] arr) { // count number of even numbers in the array int even_count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] % 2 == 0) { even_count++; } } // initialize new array with size of even_count int[] result = new int[even_count]; int index = 0; // copy all even numbers to the new array for (int i = 0; i < arr.length; i++) { if (arr[i] % 2 == 0) { result[index++] = arr[i]; } } return result; } }
Output: After removing the odd elements the array is : 2 4 6
Method-2: Java Program to Remove Odd Numbers from Array By Dynamic Initialization of Array Elements
Approach:
- Ask use length of the array.
- Initialize the array with given size.
- Ask the user for array elements.
- Count the number of even elements using a for loop.
- Initialize an array with size of the number of even elements.
- Iterate the original array and insert the even elements into the new array.
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // initializing array // create scanner class object Scanner sc = new Scanner(System.in); // take input from user for array size System.out.println("Enter the size of array: "); int n = sc.nextInt(); // initialize array with size n int[] arr = new int[n]; // take input from user for array elements System.out.println("Enter array elements: "); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } System.out.println("Array before removing odd numbers: "); printArray(arr); int[] result = removeOdd(arr); // print the array System.out.println("\nArray after removing odd numbers: "); printArray(result); } public static void printArray(int[] result) { for(int i:result) System.out.print(i + " "); } public static int[] removeOdd(int[] arr) { // count number of even numbers in the array int even_count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] % 2 == 0) { even_count++; } } // initialize new array with size of even_count int[] result = new int[even_count]; int index = 0; // copy all even numbers to the new array for (int i = 0; i < arr.length; i++) { if (arr[i] % 2 == 0) { result[index++] = arr[i]; } } return result; } }
Output: Enter the size of array: 9 Enter array elements: 1 2 3 4 5 6 7 8 9 Array before removing odd numbers: 1 2 3 4 5 6 7 8 9 Array after removing odd numbers: 2 4 6 8
Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.
Also try: How To Remove Odd Numbers From An Array
Related Java Programs: