import java.util.*; // related posts: // old (but fast) school array // Thread safe vectors // // public class Main { public static void main(String[] args) { // arraylists do not require a size definition // when you declare or instantiate. // you can add to your hearts content ArrayList al = new ArrayList(); al.add("number one"); // display the size of the ArrayList System.out.println(al.size()); // indicates: 1 // fill up with 10 for (int i=0; i<10; i++){ // you'll notice that it just appends to // the existing list. "number one" remains // and all the numbers are tacked on after al.add(i); } // display all ten with foreach //for (Object o:al for (int i=0; i<al.size(); i++){ System.out.println(al.get(i) + " is type " + al.get(i).getClass().toString()); } // you'll notice from the output that a Java ArrayList // can have a mixture of object types. Here we // blend Integer and String } }
Thursday, September 24, 2009
Java - how to create and use an ArrayList
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment