/**
* Title: ArrayListPlay
* Description: Simple example of using ArrayList
* @author hollingd@cs.rpi.edu
*/
import java.util.*;
class ArrayListPlay {
public static void main(String [] args ) {
// Create an ArrayList object
ArrayList x = new ArrayList();
// Populate with 10 random Integer objects
Random r = new Random();
for (int i=0;i<10;i++) {
x.add( new Integer(r.nextInt(100)));
}
// print using random access (get)
for (int i=0;i<x.size();i++) {
// Extract an object from the ArrayList and convert to int
// (could also give Integer object to println as well...
int y = ((Integer) x.get(i)).intValue();
System.out.println("x["+i+"] = " + y);
}
// print all elements of x using iterator
Iterator i = x.iterator();
while (i.hasNext()) {
int y = ((Integer) i.next()).intValue();
System.out.println( y);
}
}
}