Java Tutorial: Array and ArrayList


Posted 2020/06/02

Arrays

Arrays are a sequential collection of objects or primitive values with a fixed-size. To create an empty array use type[] var = new type[size] with type being a class name and size being an integer. Each item in the array has an index (location) starting from 0 to size-1. An item can be referenced with var[index] with index being an integer representing the location in the array.

Example

1
2
3
4
5
int[] arr = new int[3]; //make integer array with size of 3
arr[0] = 1; //set value of index 0 to 1
arr[1] = 2;
arr[2] = 3;
System.out.println(arr[1]); //print value in index 0

Arrays can also be initialized using array literals to assign the array with values

1
int[] arr = new int[]{1,2,3};

The length of an array can be found with its length variable which can be useful when traversing through the array.

1
System.out.println(arr.length);

Simple Array Traversal

This is a simple array traversal that uses a for loop

1
2
3
for(int i=0;i< arr.length;i++){ //loop from 0 to length-1 of arr
    System.out.println(arr[i]); //print index i
}

Enhanced Traversal

Traversal can also be done with a for-each loop. This makes traversal simpler to program but you lose access to item indexes.

1
2
3
for(int i:arr){ //loop i through all values in arr
    System.out.println(i);
}

Multi-dimensional Arrays

Multi-dimensional arrays are arrays of arrays. When creating multi-dimensional arrays add as many sets of square-brackets as the amount of dimensions you are trying to setup.

1
int[][] twoDarr = new int[2][2]; //create empty 2x2 2d array

Traversing multi-dimensional arrays can be done in a specific dimension or through all the items of the array. To traverse through all of the items, use nested for loops

1
2
3
4
5
for(int x=0; x < twoDarr.length; x++){ //loop through first dimension
    for(int y=0; y < twoDarr[x].length; y++){ //loop through second dimension
        System.out.println(twoDarr[x][y]); //print values
    }
}

When traversing through a specific dimension, use a loop on one dimension and set the indexes on the others

1
2
3
for(int x=0; x < twoDarr.length; x++){ //loop through first dimension
    System.out.println(twoDarr[x][0]); //print first index of each subarray
}

ArrayLists

ArrayLists are a sequential collection of objects of a single type with a dynamic size. When using primitive types you have to reference its class name. To use ArrayLists you have to import the java.util library. ArrayLists can be created with ArrayList<Class> var new ArrayList<Class>();

1
2
ArrayList<Integer> intList = new ArrayList<Integer>();
ArrayList<String> strList = new ArrayList<String>();

Like arrays, ArrayLists use an indexing system that goes from 0 to size()-1. However the size of the ArrayList is dynamic and can be found with the size() method.

1
System.out.println(strList.size());

Adding values to the end of the ArrayList can be done with the add(item) method. Note that this will increase the size by 1.

1
intList.add(1); //add value 1 to end of list

We can also add items at a specific index within the ArrayList with add(idx, item). Note that all items in the ArrayList starting at idx and higher will be shifted up one index.

1
intList.add(0,0); //set index 0 to 0 and shift

To access values at a specific index use the get(idx) method

1
System.out.println(intList.get(1));

You can override existing values in the ArrayList with set(idx, item)

1
intList.set(1,-1); //set index 1 to -1

Deleting a value in the ArrayList can be done with remove(idx). All items after idx will have their index decreased by 1 and the size of the ArrayList will shrink

1
intList.set(1,-1); //set index 1 to -1

Traversal

Traversal with ArrayLists is exactly the same as traversing arrays but when using a for-each loop remember to use the class name instead of primitive types

1
2
3
4
5
6
7
for(Integer value: intList){ //for-each loop
    System.out.println(value);
}

for(int i=0; i < strList.size(); i++){ //for loop
    System.out.println(strList.get(i));
}