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 the index-based methods to insert, update, delete and search the elements. It can also have duplicate elements.
Below is a demonstration of the same −
Suppose our input is −
Input list :[Java, Python, JavaScript, Shell, Scala]
The desired output would be −
The first half of the list is: [Java, Python] The second half of the list is: [JavaScript, Shell, Scala]
Algorithm
Step 1 - START Step 2 - Declare three array list namely input_list, first_list, second_list. Step 3 - Define the values. Step 4 - Get the size of the array using the function .size(). Step 5 - Iterate the input_list using a for-loop, add all the elements with index lesser the size/2 index value to the first_list and add all the elements with index greater the size/2 index value to the second_list using the .add() function. Step 6 - Display the result Step 7 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
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]
Example 2
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]