Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Split a Vector into Multiple Smaller Vectors in Java
Next article icon

Split a Vector into Multiple Smaller Vectors in Java

Last Updated : 09 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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);     } } 

Output
First 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);     } } 

Output
Odd 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.


Next Article
Split a Vector into Multiple Smaller Vectors in Java
author
21211adygf
Improve
Article Tags :
  • Java
  • Java Programs
  • Java-Collections
  • Java-Vector
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

    How to Split an ArrayList in Multiple Small ArrayLists?
    In Java, ArrayList is a pre-defined class of the Java collection framework, and it can be used to add the elements dynamically. One more special feature is that it can shrink the size dynamically to add or remove the elements from the ArrayList. In this article, we will discuss how to split an Array
    2 min read
    Java Program to Search an Element in Vector
    A vector in Java is a dynamic array that can be resized as needed. It is synchronized, which means it is safe in multi-threaded programs. To find an element in a Vector we have to loop through its elements to find a match. In this article, we will learn how to search for a component in a Vector usin
    3 min read
    Copy Elements of One Java Vector to Another Vector in Java
    Vector is similar to arrays but is growable also, or we can say no fixed size is required. Previously vector was a part of legacy classes but now it is part of Collections. It also implements a List interface, so we can use any method of list interface on vectors also. Syntax : Vector<Integer>
    3 min read
    How to Split a String into Equal Length Substrings in Java?
    In Java, splitting a string into smaller substrings of equal length is useful for processing large strings in manageable pieces. We can do this with the substring method of the loop. This method extracts a substring of the specified length from the input string and stores it in a list.Example:In the
    3 min read
    How to Replace an Element at a Specific Index of the Vector in Java?
    The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in the java.util package and implements the List interface, so we can use all the methods of List interface here. Examples Input : Vector= ["
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences