View RSS Feed

Algorithm and Data Structure

Accessing Items from a List

Rate this Entry
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.
Java Code:
System.out.println(MyList.size());
Get Item:

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.
Java Code:
System.out.println(MyList.get(0));
//or to store the value first
temp = MyList.get(0);
System.out.println(temp);
First item will be printed out in our list. However, removal from list would not be done.
Also To get 1st & last elements, these 2 methods would be used.
1. getLast()
2. getFirst()

Submit "Accessing Items from a List" to Facebook Submit "Accessing Items from a List" to Digg Submit "Accessing Items from a List" to del.icio.us Submit "Accessing Items from a List" to StumbleUpon Submit "Accessing Items from a List" to Google

Comments