Pre-condition: if you're reading this and intend to test it, you MUST have JDK 5 or later, since the implementation of the ArrayList class was changed in JDK 5.
ArrayList(s) and Static Array(s). They have a lot in common. They both store data, they can both be traversed in loops...However, the ArrayList is MUCH more appealing in my opinion, since it can be dynamically re-sized, that is, re-sized during execution of a program, which is very nice. On the flip side, a static array has size limitations to how much space you declared them to have.
Let's get to the code, shall we?
Static ArraysThe static array. A nice little structure that holds data in a neat fashion. We can traverse through the data in a loop. It seems like a perfect thing...WRONG.
java
public class Main
{
public static void main(String[] args)
{
int[] numbers = new int[5];
}
}
This array can hold up to 5 integers. It essentially already does hold them, since it initializes them to zero. You CANNOT hold more than 5 in this data structure, or you will get an
ArrayIndexOutOfBoundsException, unless you "re-size" it...which is kind of pointless unless you back up your data, because when you re-size the old array, you lose ALL of your data.
java
Whenever you use the keyword
new in creating/"re-sizing" a static array...you lose all of the previous data. In other words, all 10 of the new numbers will be set to zero.
ArrayListThe way to declare an ArrayList follows.
java
ArrayList<CLASS> name = new ArrayList<CLASS>();
If you leave out the
<CLASS> part of it, it can hold any object. It's the same as doing this...
ArrayList<Object>...
java
import java.util.ArrayList; // necessary to access the ArrayList class
public class Main
{
public static void main(String[] args)
{
ArrayList<Integer> integers = new ArrayList<Integer>();
}
}
This
integers ArrayList will only hold data of type Integer.
Methods of the ArrayList classAdding DataWell, we have an ArrayList, let's add something to it!
java
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(10)
This adds the value 10 to our list. There is another
add() method, which is as follows.
java
This adds our number 10 as the FIRST number in our list. Everything that was at index 0 or greater is pushed back 1 index.
Basic
add() syntax:
add(OBJECT_TO_ADD, INDEX_TO_ADD_AT);The second parameter is optional, if you don't use it, the object is put in the back of the list.
Accessing DataWell, now that we use a
class for storing data, we have to use methods to get to that data. Let's use our Integer list for an example...In older times (AKA pre-JDK 5), you had to "unwrap" the Object of type Integer, because you can't use an Integer object in calculations. If you had an ArrayList to store some Object form of a simple data type (int, double, boolean, etc...), you have to call other methods, and convert from object to simple data type to get the actual value of that simple data type. Now, it's as simple as using the
get() method.

We use the method
get(INDEX) to access a particular slot in the list. This accesses the object at the specified index in the list.
java
// "numbers" is predefined
int num = numbers.get(0); // first item
Traversing ArrayListsWe can use a for loop to access/output all of our array elements.
java
// "numbers" is predefined
for (int x = 0; x < numbers.size(); x++)
System.out.println(numbers.get(x));
The previous code will access ALL of the elements, since the condition in our loop to stop is
x < numbers.size(). The
size() method just returns how many objects are in our list.
There is an alternate way to print out the list...the following.
java
System.out.println(numbers);
That, if we say we have 5 integers in our list (1, 2, 3, 4, 5), will print this out...
QUOTE
[1, 2, 3, 4, 5]
As you can see, when we normally say
println(some_object); it will normally print out a memory address. The ArrayList functions differently. It's not a jerk.
Checking if we have dataIt's very simple...The ArrayList class has a few methods that make this incredibly easy.
java
boolean empty = numbers.isEmpty();
Well, I hope you can you can see what the
isEmpty() method is going to tell us...
The next method makes searching for repeats and such very easy. As a matter of fact, this is what I used numerous times in my snippet here...
NoRepeatRandom.
java
int num = 10;
if (numbers.contains(num))
System.out.println("10 is in the list!);
Well, that's easy, if the list contains whatever you put in the
contains() method...a boolean value is returned...
true in this case, since we added the number 10 to the list before.
Well, I hope this tutorial helped! I hope I didn't sound like I was rambling. Gimme a break, it's my first tutorial.

Any feedback would be
GREATLY appreciated!
"At DIC we be card dealing, array-smashing code ninjas!

" -~- Martyr2
Buh-Bye!
This post has been edited by Locke37: 30 Jul, 2008 - 05:46 PM