Menu
Topics Index
...
`


Collections Framework > Collection Interface >
Siva Nookala - 15 Apr 2016
The List Interface extends Collection. Elements can be inserted, accessed by their index in the list. This index starts at zero. The list may contain duplicate elements.

The declaration of List Interface is :
interface List< E >
Here, E specifies type of objects that list contains. The methods defined by List :
MethodDescription
void add(int index, E obj)Inserts passed obj into the invoking list at the index. Already existing elements at that point are shifted up.
boolean addAll(int index, Collection< ?extends E > c)Inserts all the elements of c into the invoking list at the index. Already existing elements at that point are shifted up. Returns trueif the invoking list changes else returns false.
E get(int index)Returns the object stored at particularindexwithin the invoked list.
int indexOf(Object obj)Returns the index of first instance of obj in the invoked list else it returns -1.
int lastIndexOf(Object obj)Returns the index of last instance of obj in the invoked list else it returns -1.
ListIterator< E > listIterator()Returns iterator to the starting of the invoked list.
ListIterator< E > listIterator(int index)Returns iterator to the specified index of the invoked list.
E remove(int index)Removes the element at position index from the invoking list and returns the deleted element. The resulting list is compacted.
E set(int index, E obj)Assigns obj to the location specified by index within the invoking list.
List< E > subList(int start, int end)Returns a list that includes elements from start to end -1 in the invoking list.
These methods are illustrated in the following example:
ListMethods
import java.util.*;

class ListMethods
{
    public static void main(String arg[])
    {
        List<Integer> marks = new ArrayList<Integer>();
        marks.add(200);
        marks.add(210);
        marks.add(226);
        for (Integer x : marks) {
            System.out.print(x + " ");
        }
        System.out.println();
        marks.add(1, 30); // void add(int index, E obj)
        for (Integer x : marks) {
            System.out.print(x + " ");
        }
        System.out.println();
        System.out.println(marks.get(2));
        System.out.println(marks.indexOf(1));
        System.out.println(marks.remove(2)); // Removes element at index 2.
        for (Integer x : marks) // Resulting list after removing element.
        {
            System.out.print(x + " ");
        }
        System.out.println();
        System.out.println(marks.set(0, 5)); // Sets element at index 0.
        for (Integer x : marks) // Resulting list after setting the element at index 0.
        {
            System.out.print(x + " ");
        }
    
    }
}
OUTPUT

200 210 226
200 30 210 226
210
-1
210
200 30 226
200
5 30 226

DESCRIPTION

The first line is just the display of elements in the listmarks. The second line is the display of elements in the list marks after adding the element 30 at index 1. The third line the display of element at index 2 using get(). The fourth line is the display of the index of the element 1 that is 0. The fifth line is the display of the element to be removed at the index 2. The sixth line is the display of the elements after removing the element 10 from the list marks. The seventh line is the display of the element at index 0 which is being set with the new element, the old element is returned that is 1.The last line is the display of the elements of the list after setting the zeroth element with 5.

THINGS TO TRY
  • For the above list, try :
    System.out.println(marks.get(6));
  • For the above list, try :
    System.out.println(marks.indexOf(226));
  • For the above list, try :
    System.out.println(marks.add(3, "hai"));
  • For the above list, try :
    System.out.println(marks.add(3, null));

Dependent Topics : Java SortedSet Interface   Java Iterator  Java LinkedList  

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.