Arrays and ArrayLists


Table that shows the relationship between
values in Arrays/ArrayLists and their indicies

Arrays and ArrayLists are extremely powerful data structures that can be used to store large collections of data. The main point of these two data structures it to store multiple variables of the same type, referenced by one central object. Arrays or ArrayLists can be used to replace a large amount of variables. For example you can either have int x1, x2, x3 …; or int[] x = new int[100]; The main similarity between arrays and ArrayLists are that they are both 0-indexed, meaning the first element can be referenced with 0. Their main difference is that the size of an array is immutable, but the size of an ArrayList can be changed dynamically. Arrays are almost always faster than ArrayLists.


Arrays

Arrays can be declared with any type, primitive and non primitive as in the following code example:


int[] x = new int[10]; // Valid
Integer[] x = new Integer[10] // Also valid