Attic
CollectionsArrays

Overview

Contiguous heap storage, 1D operations, 2D matrices, and low-level JVM memory mechanics in Java

An array is a fixed-size, zero-indexed sequence of elements allocated contiguously in heap memory. Unlike the Collections Framework (ArrayList, HashSet), arrays are fundamental Java language constructs capable of storing primitives with zero boxing overhead as well as object references.


Roadmap

The array topics are organized from practical syntax and algorithms to low-level JVM memory mechanics:


Topics


java.util.Arrays Toolkit (Quick Reference)

The primary static toolkit provided by java.util.Arrays for searching, sorting, comparing, and manipulating arrays:

Method / PropertyParametersReturn TypeDescription / Notes
arr.length(property)intPublic final field (NOT a method). Stored in JVM object header; read via arraylength bytecode
Arrays.sort(arr)(int[] | double[] | ...)voidSorts primitives via Dual-Pivot Quicksort (O(n log n))
Arrays.sort(T[] a, Comparator c)(T[] a, Comparator c)voidSorts objects via TimSort (O(n log n), stable)
Arrays.parallelSort(arr)(int[] | double[] | ...)voidMulti-core ForkJoinPool sort for arrays > 8192 items
Arrays.binarySearch(arr, key)(arr, key)intPrecondition: array must be sorted. Returns index or -(insertion_point) - 1
Arrays.fill(arr, val)(arr, val)voidAssigns val to every slot
Arrays.equals(a, b)(a, b)booleanElement-by-element equality check
Arrays.deepEquals(a, b)(Object[], Object[])booleanDeep recursive equality check for multidimensional arrays
Arrays.toString(arr)(arr)StringFormats 1D array as "[e1, e2, ...]"; avoids hashcode [I@...
Arrays.deepToString(arr)(Object[])StringFormats multidimensional 2D/3D arrays recursively
Arrays.mismatch(a, b)(a, b)intReturns index of first differing element, or -1 if identical (Java 9+)
Arrays.stream(arr)(arr)IntStream / Stream<T>Produces primitive or object stream
Arrays.copyOf(arr, newLen)(arr, int)T[]Returns truncated or zero-padded copy
Arrays.copyOfRange(arr, from, to)(arr, int, int)T[]Half-open slice copy [from, to)
Arrays.asList(T... a)(T... a)List<T>Fixed-size list wrapper backed by the array

Quick Reference: 1D vs 2D vs Memory

Concept1D Arrays2D Arrays & MatricesMemory & JVM Internals
Physical LayoutSingle contiguous memory blockArray of reference pointers to disjoint 1D arrays16-byte object header + aligned payload
Indexingarr[i] (O(1) pointer offset)matrix[r][c] (two dereferences)Byte offset: base + 16 + (i * elementSize)
Length Fieldarr.lengthRows: matrix.length, Cols: matrix[0].lengthStored as signed 32-bit int in header
PrintingArrays.toString(arr)Arrays.deepToString(matrix)Default Object.toString() returns [I@hash
Cloningarr.clone() is deep for primitivesmatrix.clone() is a shallow copy trapOnly clones the outer row pointer array

On this page