Coding, Teaching

Java: ArrayList vs Array

* Do note in this post, I’m referring to a normal array, not the Arrays class in Java. All these are snippets of code, so remember to import the ArrayList package when you need to use the ArrayList class.

I’m going to break this down into differences and similarities.

Differences Between a Java ArrayList and Array

  1. Code for creating ArrayList and array:
    // Create an ArrayList to store String objects
    ArrayList<String> strList = new ArrayList<String>();
    // Create an array to store 5 String objects
    String[] strArray = new String[5];

    When first creating the ArrayList, its size is 0.

    For the normal array, its size is stuck at 5. So before storing any String objects within, strArray will contain null in all 5 spots.

  2. This means that the size of ArrayList is dynamic; it can increase as you add objects and decrease as you remove them. For normal arrays, it will always be fixed at the value you stated during creation. In the above example, strArray will always have a size of 5.
    System.out.println("Size of strList: " + strList.size());
    // This will print "Size of strList: 0
    System.out.println("Size of strArray: " + strArray.length);
    // This will print "Size of strArray: 5
  3. An ArrayList can only store reference data types (e.g. String, Date), while arrays can store reference data types AND primitive data types (e.g. int, double).
    // Create an array to store 6 int values
    int[] iArray = new int[6];
    // Create an array to store 8 double values
    double[] dArray = new double[8];

    Do note that int and double arrays will initially contain values 0 and 0.0 in all spaces respectively. Arrays storing reference data types like String will initially contain null, as discussed above.

  4. Adding elements into an ArrayList uses the method add. Adding elements into a normal array is just like editing what’s in that index.
    // Add Strings "Apple" and "Orange" into ArrayList strList
    // Resulting strList is ["Apple", "Orange"]
    // Insert "Pear" into the 2nd position (or index 1) of strList
    strList.add(1, "Pear");
    // Resulting strList is ["Apple", "Pear", "Orange"]
    // Add Strings "Red" and "Yellow" into array strArray
    strArray[0] = "Red";
    strArray[1] = "Yellow";
    // Resulting strArray will be ["Red", "Yellow", null, null, null]
    // Insert String "Blue" into 2nd position (or index 1) of strArray, so "Yellow" shifts to the back
    strArray[2] = strArray[1]; // Shift the value of "Yellow" to the next index
    strArray[1] = "Blue";
    // Resulting strArray will be ["Red", "Blue", "Yellow", null, null]
  5. Changing the element in a particular index in ArrayList uses the method set. Doing the same thing in a normal array looks like how it does for adding as discussed above.
    // Change the 3rd element in strList to "Banana"
    strList.set(2, "Banana");
    // Resulting strList is ["Apple", "Pear", "Banana"]
    // Change the 3rd element in strArray to "Green"
    strArray[2] = "Green"
    // Resulting strArray will be ["Red", "Blue", "Green", null, null]
  6. Removing an element from an ArrayList uses the remove method. Doing the same in a normal array would involve setting null in that spot to indicate that it is not storing anything there. This only applies to arrays storing reference data types; null cannot be stored in arrays storing primitive data types.
    // Delete String object from 1st position in strList
    // Resulting strList is ["Pear", "Banana"]
    // Delete String object from 1st position in strArray
    strArray[0] = null;
    // Resulting strArray is [null, "Blue", "Green", null, null]
  7. Getting the size of an ArrayList simply uses the size method. Because using length on a normal array does not tell you how many positions are not storing null (and are being occupied by actual objects), you need to keep a counter or use a for loop to check on the spot.
    System.out.println("Size of strList: " + strList.size());
    int strArrSize = 0;
    for(int i = 0; i < strArray.length; i++) {
    	if(strArray[i] != null) {
    System.out.println("Size of strArray: " + strArrSize);

Similarities Between a Java ArrayList and Array

  1. Both can only store one type of data. The data type to store is stated during the creation of either ArrayList or the array. In the code example above, both strList and strArray can only store String objects.

    This is unlike languages like Python, that allows you to store different data types within a list (behaves like an array in Java).

  2. Position of elements in both follow the same format: start counting from index 0. Do note in the code example below, you will encounter an error (more specifically IndexOutOfBoundsException) for the ArrayList example if you don’t have at least 3 String objects inside. You won’t get an error for the normal array example as long as the index does not go beyond its (length - 1), but you will print out null instead, if you haven’t set any String object in its place.
    // Code to add items into strList and strArray
    // Retrieve the 3rd String object from ArrayList strList and print it
    // Retrieve the 3rd String object from array strArray and print it

    Index is 2 for the 3rd item, because you are counting from 0.

Leave a Reply

Your email address will not be published. Required fields are marked *