
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java program to split the Even and Odd elements into two different lists
To split the Even and Odd elements into two different lists, the Java code is as follows −
Example
import java.util.Scanner; public class Demo{ public static void main(String[] args){ int n, j = 0, k = 0; Scanner s = new Scanner(System.in); System.out.println("Enter the number of elements required :"); n = s.nextInt(); int my_arr[] = new int[n]; int odd_vals[] = new int[n]; int even_vals[] = new int[n]; System.out.println("Enter the elements of the array(even and add numbers) :"); for(int i = 0; i < n; i++){ my_arr[i] = s.nextInt(); } for(int i = 0; i < n; i++){ if(my_arr[i] % 2 != 0){ odd_vals[j] = my_arr[i]; j++; } else { even_vals[k] = my_arr[i]; k++; } } System.out.print("The odd numbers in the array : "); if(j > 1){ for(int i = 0;i < (j-1); i++){ if(odd_vals[i]==1){ System.out.println("1 is niether even nor odd"); } else System.out.print(odd_vals[i]+","); } System.out.print(odd_vals[j-1]); } else { System.out.println("There are no odd numbers."); } System.out.println(""); System.out.print("The even numbers in the array : "); if(k > 1){ for(int i = 0; i < (k-1); i++){ if(even_vals[i]==1){ System.out.println("1 is niether even nor odd"); } else System.out.print(even_vals[i]+","); } System.out.print(even_vals[k-1]); } else { System.out.println("There are no even numbers in the array."); } } }
Output
Enter the number of elements required : Enter the elements of the array(even and add numbers) : The odd numbers in the array : 1 is niether even nor odd 9 The even numbers in the array : 2,4,6
Console input
5 1 2 4 6 9
A class named ‘Demo’ contains the main function that asks for the number of elements that should be stored in the array and declares two new arrays that will store the odd values and even values respectively. The array elements are taken from the user and a ‘for’ loop is run to check if the number is divisible by 0, i.e checking if the remainder when the number is divided by 2 is 0. If yes, then that number from the main array will be stored in the even array, and in the odd array otherwise. Since 1 is neither even nor odd, it prints the message while storing 1 in the even or odd array.
- Related Articles
- Python program to split the even and odd elements into two different lists.
- Python Program to Put Even and Odd elements in a List into Two Different Lists
- C# program to split the Even and Odd integers into different arrays
- Separate Odd and Even Elements into Two Separate Arrays in Java
- Java Program to Split a list into Two Halves
- C program to store even, odd and prime numbers into separate files
- Simple way to find if two different lists contain exactly the same elements in Java
- Java program to print odd and even number from a Java array.
- Java program to Print Odd and Even Number from an Array
- Sorting odd and even elements separately JavaScript
- Java Program to Join Two Lists
- Java Program to Merge two lists
- Swap Even Index Elements And Odd Index Elements in Python
- Check Average of Odd Elements or Even Elements are Greater in Java
- Golang program to split a slice into two halves

Advertisements