How to Get Sub List of Vector in Java


In this example, we may learn how to get sub list of Vector with subList method by providing start and end index in Java.

Source Code

package com.beginner.examples;

import java.util.List;
import java.util.Vector;

public class GetSubListOfJavaVector {

	public static void main(String[] args) {
		
		//create Vector.
	    Vector vector = new Vector();
	    
	    vector.add("A");
	    vector.add("B");
	    vector.add("C");
	    vector.add("D");
	    
	    /*
	     * To get a sub list of Java Vector use subList(int startIndex, int endIndex) method.
	     */
	    List list = vector.subList(1,4);
	    
	    System.out.println("Sub list contains : ");
	    for(int i=0; i < list.size() ; i++) {
	    	System.out.println(list.get(i));
	    }
	      
	}

}

Output:

Sub list contains : 
B
C
D

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments