CKTIS / Java
Java arrays: declaration, indexing, and length
Declare and create arrays, read and replace elements by index, and distinguish array length from list size. See what `new List[2]` creates and how to avoid raw `List` in new code.
Version: Java SE 21
Syntax
int[] counts = new int[2];
Integer[] values = {1, 2, 3};
int length = values.length;
int index = 1;
Integer value = values[index];
values[index] = 4;
for (int n : values) { /* use n */ }
// With import java.util.List:
List[] buckets = new List[2];
buckets[index] = List.of("Java");
List bucket = buckets[index];
List<?>[] wildcardBuckets = new List<?>[2];int[] counts; declares a variable that can hold an array reference; it does not create an array. new int[2] creates two zero-initialized slots, while Integer[] values = {1, 2, 3}; creates and fills an array of Integer references. An array of references created without explicit element values starts with null in every slot. An array object's length is fixed: values.length is an int field, not a method. Its elements can still be replaced.
values[index] reads an element; values[index] = 4 replaces it. The valid range is 0 <= index && index < values.length. An out-of-range index causes ArrayIndexOutOfBoundsException; indexing a null array reference causes NullPointerException. Use an indexed loop when you need positions or want to replace elements. The enhanced loop for (int n : values) visits elements in index order and unboxes each Integer to int; unboxing null causes NullPointerException.
In List[] buckets = new List[2];, List from java.util is raw: it has no type argument. The expression creates an array with two null slots, not two empty lists. buckets[index] reads a list reference, and buckets[index] = List.of("Java") stores one. Only after that assignment can you call .get(0) on the list in that slot. buckets.length counts array slots; buckets[index].size() counts elements in the list stored there.
You can declare List<String>[], but new List<String>[2] is a compile-time error: because of type erasure, the JVM cannot check the String type argument on an array store. List<String>[] typed = new List[2]; compiles with an unchecked conversion warning; a type mismatch can surface later as a ClassCastException when reading a list element as a String. Use new List<?>[2] if the list element type can remain unknown, or use List<List<String>> when every list should hold strings.
Reference arrays are covariant: Object[] alias = new String[1]; is legal, but alias[0] = 42; throws ArrayStoreException because the actual array is still a String[]. An int[] is an object, but it is not a subtype of Object[].
Examples
import java.util.List;
public class ArrayDemo {
public static void main(String[] args) {
Integer[] values = {1, 2, 0};
values[2] = 3;
int sum = 0;
for (int n : values) {
sum += n;
}
System.out.println("sum=" + sum);
List[] buckets = new List[2];
int index = 1;
System.out.println("length=" + buckets.length);
System.out.println("before=" + buckets[index]);
buckets[index] = List.of("Java");
System.out.println("first=" + buckets[index].get(0));
}
}Result
sum=6 length=2 before=null first=Java
The program replaces an element in the `Integer[]` and sums the values to `6`. It then shows the list array’s length, the slot’s `null` value before assignment, and the first element of the assigned list. The raw `List` warnings are intentional and are not part of the program output.
Related lessons
- Java · Lesson 8 · Advanced · Version: 2
- Java · Lesson 13 · Advanced · Version: 2
- Java · Lesson 14 · Advanced · Version: 2
- Java · Lesson 15 · Advanced · Version: 2
- Java · Lesson 17 · Advanced · Version: 2
- Java · Lesson 22 · Advanced · Version: 2
- Java · Lesson 23 · Advanced · Version: 2
- Java · Lesson 31 · Advanced · Version: 2
- Java · Lesson 32 · Advanced · Version: 2
- Java · Lesson 34 · Advanced · Version: 2
- Java · Lesson 36 · Advanced · Version: 2
- Java · Lesson 37 · Advanced · Version: 2
- Java · Lesson 36 · Intermediate · Version: 2
Sources
- Java Language Specification, Java SE 21 — Chapter 10: Arrays
- Java Language Specification, Java SE 21 — §15.10: Array Creation and Access Expressions
- Java Language Specification, Java SE 21 — §4.7: Reifiable Types
- Java Language Specification, Java SE 21 — §5.1.9: Unchecked Conversion
- Java Language Specification, Java SE 21 — §5.1.8: Unboxing Conversion
- Java Language Specification, Java SE 21 — §14.14.2: The enhanced for statement
- Java SE 21 API — java.util.List
- JDK 21 — The javac Command