Split a Vector into Multiple Smaller Vectors in Java
Last Updated : 09 Feb, 2024
In the realm of Java programming, the manipulation of vectors is a common task. One particularly useful operation is splitting a vector into multiple smaller vectors. This article aims to provide a detailed guide on achieving this in Java, catering to both beginners and experienced developers.
Prerequisites
Before diving into the intricacies of splitting vectors, it's essential to ensure you have a working knowledge of Java-Programming-Basics. Familiarity with Vector-Operations and Data Structures will be advantageous. Additionally, make sure you have a Java development environment set up on your system.
How to Split a Vector into Multiple Smaller Vectors in Java?
Vectors in Java are dynamic arrays that can grow or shrink in size. Splitting a vector involves dividing it into smaller vectors, each containing a subset of the original elements. This operation is particularly useful when dealing with large datasets or when specific portions of a vector need separate processing.
To illustrate, imagine a vector with elements [1, 2, 3, 4, 5, 6]. Splitting it into two smaller vectors might result in [1, 2, 3] and [4, 5, 6].
Program to Split a Vector into Multiple Smaller Vectors In Java
Example 1: Splitting a Vector into Equal Parts
Let's start with a straightforward example of dividing a vector into two equal parts. Consider the following Java code snippet:
Java // Java Program for Splitting // Vector into Equal Parts import java.util.List; import java.util.Vector; public class VectorSplitter { public static void main(String[] args) { // Create an original Vector containing // integers using List.of() method Vector<Integer> originalVector = new Vector<>(List.of(1, 2, 3, 4, 5, 6)); // Calculate the middle index of the original vector int middleIndex = originalVector.size() / 2; // Create a new Vector containing the first // half of the elements from the original Vector Vector<Integer> firstHalf = new Vector<>( originalVector.subList(0, middleIndex)); // Create a new Vector containing the second // half of the elements from the original Vector Vector<Integer> secondHalf = new Vector<>(originalVector.subList( middleIndex, originalVector.size())); // Print the first half of the vector System.out.println("First Half: " + firstHalf); // Print the second half of the vector System.out.println("Second Half: " + secondHalf); } }
OutputFirst Half: [1, 2, 3] Second Half: [4, 5, 6]
Example 2: Splitting a Vector Based on a Condition
In some scenarios, you might want to split a vector based on a specific condition. Let's say we want to split a vector into odd and even elements:
Java // Java Program to Splitting a Vector // Based on a Condition import java.util.List; import java.util.Vector; public class ConditionalVectorSplitter { public static void main(String[] args) { // Create an original Vector containing // integers using List.of() method Vector<Integer> originalVector = new Vector<>(List.of(1, 2, 3, 4, 5, 6)); // Create new Vectors to hold odd and even elements Vector<Integer> oddVector = new Vector<>(); Vector<Integer> evenVector = new Vector<>(); // Iterate through the originalVector and // categorize elements into oddVector and evenVector for (Integer element : originalVector) { if (element % 2 == 0) { // Check if the element is even evenVector.add(element); // Add even element to evenVector } else { oddVector.add(element); // Add odd element to oddVector } } // Display the results System.out.println("Odd Elements: " + oddVector); System.out.println("Even Elements: " + evenVector); } }
OutputOdd Elements: [1, 3, 5] Even Elements: [2, 4, 6]
This example splits the vector into two smaller vectors based on whether the elements are odd or even.