List Interface:

  • List interface is part of java.util package. List interface can add value elements by add(value) method.
  • List can implement Vector, ArrayList class.
  • List value can get by Iterator interface.
  • A List is an ordered Collection (sometimes called a sequence).
  • Lists may contain duplicate elements. In addition to the operations inherited from Collection.

List Example

import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public class ListExample {

public static void main(String[] args) {

// List Example implement with ArrayList
List<String> ls=new ArrayList<String>();

ls.add(“one”);
ls.add(“Three”);
ls.add(“two”);
ls.add(“four”);

Iterator it=ls.iterator();

while(it.hasNext())
{
String value=(String)it.next();

System.out.println(“Value :”+value);
}
}
}

Output

List Value : O ne
List Value :Three
List Value :two
List Value :four


Advertisement