For primitive arrays:
int[] a = new int[]{1, 2, 3, 4, 5, 6};
or
int[] a = new int[6];
the default values are 0.
When we create arrays of a particular type, cast is needed:
Set<Integer>[] adj = (Set<Integer>)new HashSet[]{new HashSet<>(), new HashSet<>()};
or
Set<Integer>[] ajd = (Set<Integer>)new HashSet[6];
the default values are null.
ArrayList implements List interface, the implementation is based on object array. Unlike array, ArrayList's size is not fixed, it is easier to use than array as the building block. In the following example, the add(1), get(0), contains(0) time cost is O(1), the add(0, 2) time cost is O(N), the space cost of ArrayList is O(N).
Code:
import java.util.*;
public class test {
public static void main(String...args) {
List<Integer> a = new ArrayList<>();
a.add(1); //append element at end of list
a.add(0, 2); //insert the element at the specified index, shift the elements right to empty a slot
a.get(0); //retrieve the element at the specified index
System.out.println(a.contains(0));
}
}