Guide to ArrayLists

ArrayList is a class built into Java that emulates an Array but gives you the ability to grow or shrink while an Array cannot grow or shrink.

To use it, import java.util.*;

ArrayLists store all data in the form of Objects.  So int, double and boolean values need to be put into an Object type of some kind to be stored into an ArrayList.  Since all classes subclass Objects in some way, they all can be stored into an ArrayList.

To declare an ArrayList you can say ArrayList myList = new ArrayList(); where myList is the name of the variable for the ArrayList.

The main methods you need to be away of for ArrayLists are as follows:
This returns an item from the ArrayList at the given index (this starts counting at 0)
This sets a value in the ArrayList at the given index to be the Object specified
This removes the Object at index n and shrinks the size of the ArrayList by 1
This returns an int that specifies how many items the ArrayList has
This adds an Object to the end of the ArrayList.  This also grows the size of the ArrayList by 1.  this operates in O(1) time
This adds Object o into the ArrayList at position n.  This will shift all items past item n one higher, and thus increase the size of the ArrayList by 1.  This operates in O(n) time

Notes:  since you can only put Objects into ArrayLists, you have to package ints, doubles and booleans inside of Objects.  Fortunatly, Java provides us with simple "wrapper" classes to accomplish this.  For boolean values you can put them into Boolean objects.  For int values you can place them into Integer objects, and for double values, you can store them in Double objects. 

Information on the wrapper classes:

Integer:
To declare an Integer object, you say new Integer(the int value), so if you wanted to create an Integer storing 5, you would say this:
Integer f  = new Integer(5);
To get the value back out of the Integer, use the intValue() method.  For example:
int a = f.intValue();

Double:
To declare a Double object, you say new Double(the double value), so if you wanted to create a Double storing 5.5, you would say this:
Double d = new Double(5.5);
To get the value back out of the Double, use the doubleValue() method.  For example:
double a = d.doubleValue();

Boolean:
To declare a Boolean object, you say new Boolean(the boolean value), if you wanted to create a Boolean storing false, you would say this:
Boolean b = new Boolean(false);
To get the value back out of the Boolean, use the booleanValue() method.  For example:
boolean a = b.booleanValue();


How to retrieve data from an ArrayList:

When you retrieve a value from an ArrayList using get, something odd happens.  You actually get an Object back!  What if you put in an Integer or something else?  Well, now Java no longer knows all the special details about it.  As far as it can tell, it's just an Object, so Java will now complain if you try to call the methods that it should know.  This cannot stand! 
You need to tell Java what type it really is.  Do this via casting the result of get. 
Let's say you have an ArrayList called frank that stores Integers.  To return the int stored in location 5, you would need to do the following:

((Integer) frank.get(5)).intValue();

The parethesis are important here.  You enclose (Integer) frank.get(5) in parenthesis so Java knows to treat the result of frank.get(5) as an Integer.  Once Java knows to treat it as an Integer, it will know what intValue() does. 

An example of arrays vs ArrayLists

To sort an array of ints, you could write a method like this:

void sort(int[] a) {
for (int y = a.length-1; y > 1; y--)
for (int x = 0; x < y; x++)
if (a[x] > a[x+1]) {
int temp = a[x];
a[x] = a[x+1];
a[x+1] = temp;
}
}

The equivlent in ArrayLists is as follows:

void sort(ArrayList a) {
for (int y = a.size()-1; y > 1; y--)
for (int x = 0; x < y; x++)
if(((Integer)a.get(x)).intValue() > ((Integer)a.get(x+1)).intValue()) {
Integer temp = (Integer) a.get(x); 
a.set(x, a.get(x+1));
a.set(x+1,temp);
}
}  


Copyright (C) 2004 Jim Casaburi