SwadhyAI Notes

All Java notes

Java arrays

›Short versionOpen if you only have a minute.
  • An array has a fixed number of slots of one type. The number is chosen when the array is created.
  • Indexes start at 0. The last legal index is length - 1. length itself is not a legal index.
  • i <= array.length steps off the end and throws ArrayIndexOutOfBoundsException. The test is i < array.length.
  • length is a field. cars.length() does not compile. A string’s length is the method length().
  • new int[3] fills the slots with 0. new String[3] fills them with null. Two variables can refer to one array.

An is a row of slots you can reach by . The row does not grow. The decision that matters on the first loops you write is the : which indexes exist, and which one is the first that does not.

java
String[] cars = {"Volvo", "BMW", "Ford"};
int[] counts = new int[3];
String[] names = new String[3];

System.out.println(cars[0]);
System.out.println(counts[0]);
System.out.println(names[0]);

The prints are Volvo, then 0, then null. The brace list stores the values you wrote and sets the length from how many there are. new int[3] stores the zero value of an . new String[3] stores null in each slot. names[0].length() throws NullPointerException until you store a string in that slot.

The last slot, and the number just past it

tells you how many slots exist. Reaching a slot is a separate question. cars[0] = "Opel"; replaces the first element. The last element of a three-element array is cars[2], which is cars[cars.length - 1].

java
String[] cars = {"Volvo", "BMW", "Ford"};
cars[0] = "Opel";
System.out.println(cars[0]);
System.out.println(cars.length);

for (int i = 0; i < cars.length; i++) {
    System.out.println(cars[i]);
}

What the console shows

Opel
3
Opel
BMW
Ford

Two names, one row

The variable is a . Copying the variable copies the reference. A second array, with its own slots, is a different object you have to create.

java
int[] a = {1, 2, 3};
int[] b = a;
b[0] = 9;
System.out.println(a[0]);

int[][] grid = {
    {1, 2},
    {3, 4}
};
System.out.println(grid[1][0]);

The first print is 9. b[0] = 9 wrote into the only row, and a reads that same slot. The second print is 3: row 1, column 0. A two-dimensional array is an array whose elements are arrays. grid.length is the number of rows. grid[1].length is the number of slots in that row. Each of those indexes still starts at 0 and stops before its own length.

cars.length() does not compile, because length on an array is a field. The parentheses belong to String.length(). Swapping them is a compile error in one direction and a method call in the other, so the message depends on which value you actually have.

int[] a = {1, 2, 3}; int[] b = a; b[0] = 9; What does a[0] print, and why?

It prints 9. b = a copies the reference, so both names refer to the same array object. Writing b[0] changes that shared slot. A separate row would be a new array, filled by copying each element, and then b[0] = 9 would leave a[0] at 1.