Java – List Example

 

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.

List example give a method, how to use List in java.

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

Tags:

 

3 Responses to “Java – List Example”

  1. maheshbabu says:

    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 ls=new ArrayList();

    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);
    }
    }
    }

Leave a Reply