UNIT 4: DATA COLLECTIONS · TOPIC 4.3
4.3 Array Creation and Access
Arrays are fixed-size, indexed from 0. Off-by-one and out-of-bounds are the whole story.
What you need to know
- Declare and create:
int[] nums = new int[5];— five ints, all initialized to 0.double[]→ 0.0,boolean[]→ false, object arrays (String[]) → null. - Initializer list:
int[] nums = {4, 8, 15};creates and fills in one step. - Access:
nums[i]. Indices run from 0 tonums.length - 1.lengthis a field (no parentheses), unlike String'slength(). - Accessing index
lengthor any negative index throws ArrayIndexOutOfBoundsException at run time. - Array size is fixed once created. To "grow" you create a new, bigger array and copy.
- Arrays are objects: an array variable holds a reference.
int[] b = a;makes b an alias of a — same array. - An array of objects holds references; each element must be assigned an object before its methods are called, or you get a NullPointerException.
Worked example
int[] a = new int[4]; // {0, 0, 0, 0}
a[0] = 7;
a[3] = 2;
a[a.length - 1] = 9; // last element → 9
System.out.println(a[2]); // 0
System.out.println(a.length);// 4
// a[4] = 1; // ArrayIndexOutOfBoundsException
String[] words = new String[2];
// words[0].length(); // NullPointerException — element is null
words[0] = "hi";
System.out.println(words[0].length()); // 2
Exam tip: Every array question: what's the first index (0), the last index (length − 1), and what's the default value? An index equal to length is the exam's favorite exception. And
length without parentheses — with parentheses it won't compile.Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- Two creation forms:
new int[n]— n elements, all default;{a, b, c}— initializer, exactly those elements. You can't use an initializer after the declaration line (arr = {1, 2};won't compile); usearr = new int[]{1, 2};. - Defaults: 0, 0.0, false, null. An array of Strings or of your own class starts as all nulls; you must assign each element an object before calling methods on it.
new Student[5]creates zero Students. - length is a field:
arr.length, no parentheses. (String's is a method:s.length(). ArrayList's islist.size().) Mixing these is a compile-error question. - Valid indices: 0 through length − 1. Index length is one past the end — the exam's favorite exception. Negative indices also throw.
- Arrays are objects. The variable holds a reference.
int[] b = a;aliases; modifyingb[0]modifiesa[0]. To copy, make a new array and loop. - Fixed size forever. To add an element you create a bigger array, copy everything, and switch the reference. This is what ArrayList does internally — which is why ArrayList exists.
- Array of arrays = 2D array (4.11). Each row is a 1D array object with its own length.
- Passing an array to a method passes the reference: the method can change elements, not replace the array (3.6).
Mistakes that cost points
- arr[arr.length]. Out of bounds. Last element is length − 1.
- arr.length(). Compile error. No parentheses for arrays.
- Calling a method on an element of a new object array. It's null. NullPointerException.
- Expecting
int[] b = a;to copy. It aliases.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 Which of the following will cause a run-time exception?
int[] arr = new int[6];
Show answer
Answer: C. Valid indices are 0–5. Index 6 is out of bounds.
Q2 What is printed by the following code?
int[] x = {3, 6, 9};
int[] y = x;
y[1] = 0;
System.out.println(x[1]);Show answer
Answer: B. y is an alias of x; they're the same array. Changing y[1] changes x[1].
Q3 After
double[] d = new double[3];, what is the value of d[1]?Show answer
Answer: B. Numeric arrays are initialized to zero.
Key vocabulary
- Array
- a fixed-size, ordered collection of elements of one type
- Index
- an element's position, from 0 to length - 1
- length
- the field giving the number of elements in an array
- ArrayIndexOutOfBoundsException
- the run-time error from using an invalid index