Accessing Items from a List
by, 02-19-2012 at 06:50 AM (535 Views)
Get List Size:
To get the List size, method “size()”is used. Size of MyList will be printed by this code.
Get Item:Java Code:System.out.println(MyList.size());
For getting a specific spot value, use get() method. In this method index of the List is provided. In case when index is not present, IndexOutOfBoundsException gets thrown.
Element is not removed from the list by this method. Just this object is returned. This given code will be getting the List element & will be displaying it.
First item will be printed out in our list. However, removal from list would not be done.Java Code:System.out.println(MyList.get(0)); //or to store the value first temp = MyList.get(0); System.out.println(temp);
Also To get 1st & last elements, these 2 methods would be used.