BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to get sub list of Vector example in java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this example, we are gonna see how to get a sublist of elements from a Vector. We will be using subList() method of Vector class to do this.
More about this method from javadoc:
public List subList(int fromIndex, int toIndex): It returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned List is empty.) The returned List is backed by this List, so changes in the returned List are reflected in this List, and vice-versa. The returned List supports all of the optional List operations supported by this List.
This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a List can be used as a range operation by operating on a subList view instead of a whole List. For example, the following idiom removes a range of elements from a List:
list.subList(from, to).clear();

Example

import java.util.Vector;
import java.util.List;
 
public class SublistExample {
 
  public static void main(String[] args) {
 
    // Step 1: Create a Vector
    Vector<String> vector = new Vector<String>();
    
    // Step 2: Add elements
    vector.add("Item1");
    vector.add("Item2");
    vector.add("Item3");
    vector.add("Item4");
    vector.add("Item5");
    vector.add("Item6");
    /* The method subList(int fromIndex, int toIndex) 
     * returns a List having elements of Vector 
     * starting from index fromIndex 
     * to (toIndex - 1).
     */
    List subList = vector.subList(2,5);
 
    System.out.println("Sub list elements :");
    for(int i=0; i < subList.size() ; i++){
       System.out.println(subList.get(i));
    }
  }
}

Output:

Sub list elements :
Item3
Item4
Item5

Top Related Articles:

  1. ArrayList clone() method in Java
  2. Difference between HashMap and Hashtable
  3. Java – Add element at specific index in LinkedList example
  4. Examples of throws Keyword in Java
  5. Java – Convert Vector to ArrayList example

Tags: Collections, Java-Vector

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Rehan Usmani says

    March 17, 2016 at 12:04 PM

    After running the above program . : Exception in thread “main” java.lang.Error: Unresolved compilation problems:
    subList cannot be resolved
    subList cannot be resolved

    at controlFramework
    VectorSubListExample
    main(VectorSubListExample.java:16)

    Reply
    • Rajeshwari says

      June 28, 2016 at 7:32 PM

      change the variable name List sublist =vector.subList(2,5);

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap