Friday, November 2, 2012

Java Array List Example

This simple program show's how we can use Java's array list to store and access individual elements through an iterator

package com.giri.test;

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

public class ListExample {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  ArrayList list = new ArrayList();
  list.add("one");
  list.add("Two");
  list.add("Three");
  list.add("Four");
  
  Iterator it = list.iterator();
  int count = 0;
  
  while(it.hasNext()) {
   String value = (String) it.next();
   System.out.println("index("+count+") , Value: "+ value);
   count++;
  }
 }

}

Output:

index(0) , Value: one
index(1) , Value: Two
index(2) , Value: Three
index(3) , Value: Four

No comments :

Post a Comment