* 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
- 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 containnull
in all 5 spots. - 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
- 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
anddouble
arrays will initially contain values 0 and 0.0 in all spaces respectively. Arrays storing reference data types likeString
will initially containnull
, as discussed above. - Adding elements into an
ArrayList
uses the methodadd
. Adding elements into a normal array is just like editing what’s in that index.// Add Strings "Apple" and "Orange" into ArrayList strList strList.add("Apple"); strList.add("Orange"); // 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]
- Changing the element in a particular index in
ArrayList
uses the methodset
. 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]
- Removing an element from an
ArrayList
uses theremove
method. Doing the same in a normal array would involve settingnull
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 strList.remove(0); // Resulting strList is ["Pear", "Banana"] // Delete String object from 1st position in strArray strArray[0] = null; // Resulting strArray is [null, "Blue", "Green", null, null]
- Getting the size of an
ArrayList
simply uses thesize
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 afor
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) { strArrSize++; } } System.out.println("Size of strArray: " + strArrSize);
Similarities Between a Java ArrayList and Array
- 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, bothstrList
andstrArray
can only storeString
objects.This is unlike languages like Python, that allows you to store different data types within a list (behaves like an array in Java).
- 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 theArrayList
example if you don’t have at least 3String
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 outnull
instead, if you haven’t set anyString
object in its place.// Code to add items into strList and strArray ... // Retrieve the 3rd String object from ArrayList strList and print it System.out.println(strList.get(2)); // Retrieve the 3rd String object from array strArray and print it System.out.println(strArray[2]);
Index is 2 for the 3rd item, because you are counting from 0.