An array is a fixed-length object that holds a numbered sequence of elements of one type. Indexes start at 0 and end at length - 1. length is a final field, not a method (that is String.length()).
int[] is not a List<Integer>.Mental model: int[] a is a pointer. a[i] is a slot. new int[n] allocates n slots that cannot grow. To grow, allocate another array and copy.
int[] a; // reference, unassigned local
int[] b = new int[4]; // {0,0,0,0}
int[] c = {1, 2, 3}; // length 3
int[] d = new int[] {1, 2, 3};
String[] names = new String[2]; // {null, null}
int a[] is legal C-style and not used in new code. Write int[] a.{…} shortcut works only in a declaration (or as new int[]{…} elsewhere).new int[-1] throws NegativeArraySizeException.new int[0] is a real object of length 0 — useful as “no values” without null.// Incorrect
int[] a;
a = {1, 2}; // does not compile
// Correct
a = new int[] {1, 2};
a[i]. The index expression is an int (byte/short/char promote).ArrayIndexOutOfBoundsException (not a compile error). a[a.length] is always illegal.NullPointerException on a.length or a[i].int last = a[a.length - 1]; // Incorrect: a[a.length]
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
for (int value : a) {
System.out.println(value);
}
for.for.Arrays.toString(a). System.out.println(a) prints a type-and-hash, not the contents.int[][] m is an array of arrays. Rows can have different lengths (ragged).
int[][] m = new int[2][3]; // 2 rows, each length 3
int[][] r = new int[2][];
r[0] = new int[3];
r[1] = new int[1];
int[][] literal = {
{1, 2, 3},
{4, 5, 6}
};
m.length is the number of rows. m[i].length is that row’s length. There is no m[0].length().m[i] may be null even if m is not — check before m[i].length.Arrays.deepToString(m).O(rows × cols) when rectangular.| Tool | What you get |
|---|---|
int[] b = a; | Alias — one array, two names |
a.clone() | Shallow copy of the top array |
Arrays.copyOf(a, n) | New array, length n (pad or trim) |
System.arraycopy | Bulk copy, overlapping ranges allowed |
Arrays.copyOfRange | Slice [from, to) |
Shallow: a String[] copy shares the String objects (safe — they are immutable). A StringBuilder[] copy shares the builders (mutations show in both). A 2D clone() copies the row references, not the rows.
// Incorrect — alias int[] b = a; b[0] = 9; // a[0] is 9 // Correct — independent ints int[] b = Arrays.copyOf(a, a.length);
You must still be able to write the hand versions. Then use the library in production.
| Need | Library | Hand idea |
|---|---|---|
| Sort | Arrays.sort(a) | Selection / insertion for exams; never bubblesort in prod |
| Search sorted | Arrays.binarySearch | Low/high mid — know it returns insertion point when missing |
| Fill | Arrays.fill | Indexed loop |
| Equals | Arrays.equals / deepEquals | == is identity |
| Min / max / sum | Streams or a loop | One pass, seed with a[0] after a length check |
int min = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] < min) {
min = a[i];
}
}
Arrays.sort on objects needs Comparable or a Comparator. On primitives it sorts ascending. It is not a stable sort for primitives (implementation detail you should not depend on).
== on two arrays: identity, not contents.println(a) and thinking it is empty because you see [I@….Object[] xs = new String[1]; xs[0] = 1; → ArrayStoreException.new String[3] holds nulls — calling methods on names[0] NPEs.List.length is a field. Indexes are 0 .. length-1.List in APIs; arrays for primitives, interop, or a measured hot path.Arrays.toString / equals / sort instead of ad-hoc bugs.~ Good luck with your revision! ~