April 4, 2011

Java Collections Framework: List Example


package info.icontraining.collections;

import java.util.*;

public class ListExample {

   public static void main(String[] args) {

      List list = new ArrayList();
  
      list.add("Be");
      list.add("The");
      list.add("Change");
      list.add("You");
      list.add("Wish");
      list.add("Wish");
      list.add("To");
      list.add("See.");
  
      System.out.println("Size: " + list.size());
  
      System.out.println("List Contains \"You\": " + list.contains("You"));
      System.out.println("Index 6: " + list.get(6));
      System.out.println("indexOf \"Change\": " +list.indexOf("Change"));
  
      Iterator iterator = list.iterator();
      for( ; iterator.hasNext(); ) {
         System.out.print(iterator.next() + " ");
      }
  
      System.out.println("toString(): " + list.toString());
  
      list.clear();
      System.out.println("Size after clear(): " + list.size());
   }
}

No comments:

Post a Comment