Java Vector subList() Method24 Mar 2025 | 2 min read 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. SyntaxFollowing is the declaration of subList() method: Parameter
ReturnThe subList() method returns a view of the specified range within the list. ExceptionsArrayIndexOutOfBoundsException- 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 VersionJava 1.2 and above Example 1Output: The vector elements are: [1, 2, 3, 4, 5, 6, 7, 8] Sublist elements: 3 4 5 6 Example 2Output: The vector elements are: [White, Green, Black, Orange, Yellow, Pink] Sublist elements :[Green, Black, Orange, Yellow] Example 3Output: 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) Next TopicVector toString() |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India