88
Java List get() Method
The get() method of List interface returns the element at the specified position in this list.
Syntax
Parameters
The parameter ‘index’ represents the index of the elements to return.
Return
The get() method returns the element at the specified position in this list.
Throws:
IndexOutOfBoundsException– If the index is out of range i.e. index<0 or index>=size().
Example 1
Output:
Element 0 stored at Index : 0 Element 1 stored at Index : 1 Element 2 stored at Index : 2 Element 3 stored at Index : 3 Element 4 stored at Index : 4 Element 5 stored at Index : 5
Example 2
Output:
ID : 17 Name :Ajeet Kumar Age : 36
Example 3
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: -1, Size: 3 atjava.util.LinkedList.checkElementIndex(LinkedList.java:555) atjava.util.LinkedList.get(LinkedList.java:476) at com.tutorAspire.JavaListGetExample3.main(JavaListGetExample3.java:12)
Next TopicJava List