4 · Data & Collections · lesson 19 of 41

Arrays

Fixed-size, contiguous, index-from-zero.

Java
int[] nums = new int[5];      // {0,0,0,0,0}
int[] primes = {2, 3, 5, 7, 11};
System.out.println(primes[2]);   // 5
System.out.println(primes.length);

int[][] grid = new int[3][3];    // multidimensional
WATCH OUT
Out-of-range access throws ArrayIndexOutOfBoundsException.
Loading 3D scene…
Key takeaways
  • ✓Length is fixed at creation.
  • ✓Multidim arrays are arrays of arrays.