Java Vector subList() Method

The subList() method of Java Vector class is used to get a view of the portion of the list between fromIndex, inclusive, and toIndex, exclusive. If fromIndex and toIndex are equal, the returned List is empty.

Syntax

Following is the declaration of subList() method:

Parameter

ParameterDescriptionRequired/Optional
fromIndexIt is the low endpoint (inclusive) of the subList.Required
toIndexIt is the high endpoint (inclusive) of the subList.Required

Return

The subList() method returns a view of the specified range within the list.

Exceptions

ArrayIndexOutOfBoundsException- This method has thrown an exception if an endpoint index value is out of range i.e. (fromIndex < 0 || toIndex > size()).

IllegalArgumentException- This method has thrown an exception if the endpoint indices are out of order, i.e. (fromIndex > toIndex).

Compatibility Version

Java 1.2 and above

Example 1

Test it Now

Output:

The vector elements are: [1, 2, 3, 4, 5, 6, 7, 8]
Sublist elements: 
3
4
5
6

Example 2

Test it Now

Output:

The vector elements are: [White, Green, Black, Orange, Yellow, Pink]
Sublist elements :[Green, Black, Orange, Yellow]

Example 3

Test it Now

Output:

Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(6) > toIndex(2)
	at java.base/java.util.AbstractList.subListRangeCheck(AbstractList.java:509)
	at java.base/java.util.AbstractList.subList(AbstractList.java:497)
	at java.base/java.util.Vector.subList(Vector.java:1152)
	at myPackage.VectorSubListExample3.main(VectorSubListExample3.java:12)





Latest Courses