
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Split a List into Two Halves in Java
In this article, we will understand how to split a list into two halves. A list is an ordered collection that allows us to store and access elements sequentially. It contains index-based methods to insert, update, delete, and search the elements. It can also have duplicate elements. We will be using different approaches to split a list into two halves ?
Problem Statement
Write a Java program to split a list into two halves. As demonstrated below ?
Input
Input list :[Java, Python, JavaScript, Shell, Scala]
Output
The first half of the list is: [Java, Python] The second half of the list is: [JavaScript, Shell, Scala]
Different Approaches
Below are the different approaches to split a list into two halves ?
Using the Main() method
The following are the steps to split a list into two halves ?
- We will start by importing ArrayList and List class from java.util package.
- Define the input_list and add elements to it.
- To hold the split halves we will create two new lists, first_list, and second_list.
- Determine the size of the input_list.
- Iterate through the first half of the input_list and add elements to first_list.
- Iterate through the second half of the input_list and add elements to second_list.
- Print the first_list and second_list.
Example
Here, we bind all the operations together under the main() method ?
import java.util.ArrayList; import java.util.List; public class Demo { public static void main(String[] args) { System.out.println("Required packages have been imported"); List<String> input_list = new ArrayList<String>(); input_list.add("Java"); input_list.add("Python"); input_list.add("JavaScript"); input_list.add("Shell"); input_list.add("Scala"); System.out.println("The list is defined as " +input_list); List<String> first_list = new ArrayList<String>(); List<String> second_list = new ArrayList<String>(); int size = input_list.size(); System.out.println("\nThe first half of the list is: "); for (int i = 0; i < size / 2; i++) first_list.add(input_list.get(i)); System.out.println(first_list); System.out.println("The second half of the list is: "); for (int i = size / 2; i < size; i++) second_list.add(input_list.get(i)); System.out.println(second_list); } }
Output
Required packages have been imported The list is defined as [Java, Python, JavaScript, Shell, Scala] The first half of the list is: [Java, Python] The second half of the list is: [JavaScript, Shell, Scala]
Using object-oriented programming
The following are the steps to split a list into two halves ?
- We will start by importing the necessary packages.
- Define a split_list function that takes the input_list as an argument.
- Inside the split_list function, create two new lists, first_list and second_list.
- Determine the size of the input_list.
- Iterate through the first half of the input_list and add elements to first_list.
- Iterate through the second half of the input_list and add elements to second_list.
- Print the contents of first_list and second_list.
- In the main function, define the input_list and add elements to it.
- Call the split_list function with input_list as the argument.
Example
Here, we encapsulate the operations into functions exhibiting object-oriented programming ?
import java.util.ArrayList; import java.util.List; public class Demo { static void split_list(List<String> input_list) { List<String> first_list = new ArrayList<String>(); List<String> second_list = new ArrayList<String>(); int size = input_list.size(); System.out.println("\nThe first half of the list is: "); for (int i = 0; i < size / 2; i++) first_list.add(input_list.get(i)); System.out.println(first_list); System.out.println("The second half of the list is: "); for (int i = size / 2; i < size; i++) second_list.add(input_list.get(i)); System.out.println(second_list); } public static void main(String[] args) { System.out.println("Required packages have been imported"); List<String> input_list = new ArrayList<String>(); input_list.add("Java"); input_list.add("Python"); input_list.add("JavaScript"); input_list.add("Shell"); input_list.add("Scala"); System.out.println("The list is defined as " +input_list); split_list(input_list); } }
Output
Required packages have been imported The list is defined as [Java, Python, JavaScript, Shell, Scala] The first half of the list is: [Java, Python] The second half of the list is: [JavaScript, Shell, Scala]