Java List sublist() MethodThe sublist() method of List Interface returns a view of the portion of this list between the inclusive and exclusive parameters. This method eliminates the need for explicit range operations. SyntaxParametersThe parameters 'fromIndex' and 'toIndex' represent the low and high endpoint of the subList. ReturnThe subList() method returns a view of the specified range within this list. Throws:IndexOutOfBoundsException- This exception will throw for an illegal endpoint index value, i.e, fromIndex < 0 or toIndex> size or fromIndex > toIndex. Example 1Test it NowOutput: List : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Elements from 2 index position to 7 index position : [3, 4, 5, 6, 7] Example 2Test it NowOutput: Total alphabets : 26 Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(18) >toIndex(12) atjava.util.SubList.<init>(AbstractList.java:624) atjava.util.AbstractList.subList(AbstractList.java:484) at com.javaTpoint.JavaListSubListExample2.main(JavaListSubListExample2.java:12) Example 3Test it NowOutput: Total alphabets : 26 Exception in thread "main" java.lang.IndexOutOfBoundsException: fromIndex = -1 atjava.util.SubList.<init>(AbstractList.java:620) atjava.util.AbstractList.subList(AbstractList.java:484) at com.javaTpoint.JavaListSubListExample3.main(JavaListSubListExample3.java:13) Next TopicJava List |
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