Plain arrays vs. ArrayList


Tim Hoffman

the ArrayList class vs. Arrays library

ArrayList class contains an array and also contains many methods that operate on the array (add() remove() .get() etc. ). ArrayList maintains its own internal count (.size() ) and resizes itself automatically as soon as the underlaying array gets full.


ArrayList<String> wordList = new ArrayList<String>();  // ArrayList
vs.
String[] wordList = new String[ 10 ]; // plain array

Notice there are several differences in how a ArrayList is declared vs. how a plain array is declared. A plain array requires the use of []s to specify a dimension for the array. An ArrayList does not require you to specify how big you want the ArrayList to be. This is because a ArrayList can change in size as you use it by letting you .add() elements to the array indefinitely. A plain array on the other hand requires you specify how big the array is and that capacity cannot be changed. The ArrayList will by default declare a plain array of a small capacity then automatically upsize that array as needed without any thought or action on your part. The most commonly used method will be the .add() method that will append a new element to the end of the array. The ArrayList provides several other methods that put new values into the array or remove elements. We will look at them shortly. Notice the <String> before the ArrayList constructor. This means your ArrayList is an array of Strings and you can only put strings into it. **You can put nulls into the list but we'll talk later about that and we don't need nulls for this assignment. An ArrayList can be defined to hold any non primitive type.

Some commonly used methods of the ArrayList are :

To ask an ArrayList how many elements are currently stored in the array you use the .size() method. To ask a plain array how many elements are currently stored in the array you use your own count variable which you have to maintain. Interstingly, an ArrayList will not let you ask it how big (capacity) the underlying plain array inside the class is. The user is not supposed to be worrying/thinking about that detail since the array is flexible and will grow as needed. You are to assume it will resize the underlying array as needed with no need for you to do anything.

The first and most obvious advantage of ArrayList over plain arrays is that you don't have to make any guesses about how big to initialize it to and you don't have to worry about running out of space or resizing it. You can just keep adding more elements to the ArrayList until your input is consumed. Lets look at how you might use anArrayList to read in all the dictionary words into your List. We will use the .add() method which is built into the ArrayList class.

Let's look at : ArrayListDemo1.java to see how to declare, use and print out the values in a ArrayList

Here are two input files use with our demo program. Put either file name on the command line: jumbles.txt OR dictionary.txt

Let's look at the ArrayList API to see the members of the the ArrayList.