Unravel โบ Java concepts โบ Collections and streams
Arrays and the Arrays utilities
An array is a fixed-size row of values of one type, indexed from 0: int[] a = new int[5].
At a glance
A row of numbered lockers: the number of lockers is fixed when the row is built, each holds one item of the agreed kind, and you open one by its number.
- 1Create with new int[5] (filled with zeros, null or false) or a literal {1, 2, 3}a.length is the size (a field, not a method). a[a.length] throws ArrayIndexOutOfBoundsException.
- 2System.out.println(a) prints a meaningless [I@1b6d3586Use Arrays.toString(a) and, for nested arrays, Arrays.deepToString.
- 3Arrays.sort(a), Arrays.fill(a, 0), Arrays.copyOf(a, n), Arrays.equals(a, b) (contents) and Arrays.asList(1, 2, 3) (a fixed-size list view) are the ones you use daily
- 4A two-dimensional array is an array of arrays: int[][] grid = new int[3][4]; and rows can have different lengths
- a == b and a.equals(b) compare identity, not contents
- Arrays are covariant and checked at runtime: Object[] o = new String[1]; o[0] = 1; compiles but throws ArrayStoreException
- An array is an object with a header, a length and the elements laid out contiguously, which makes iteration very cache friendly
import java.util.Arrays;public class Scores { static int[] top(int[] scores, int n) { int[] copy = Arrays.copyOf(scores, scores.length); Arrays.sort(copy); return Arrays.copyOfRange(copy, copy.length - n, copy.length); } public static void main(String[] args) { int[][] grid = new int[3][3]; int[] sample = {5, 3, 9, 1}; System.out.println(Arrays.toString(top(sample, 2)) + " " + Arrays.deepToString(grid)); }}- line 5 uses sort, the core of Arrays and the Arrays utilities
- line 11 uses toString, the core of Arrays and the Arrays utilities
Read the full explanation (about 1 min)
An array is a fixed-size row of values of one type, indexed from 0: int[] a = new int[5]. The Arrays class sorts, fills, copies, compares and prints them.
A mental model
A row of numbered lockers: the number of lockers is fixed when the row is built, each holds one item of the agreed kind, and you open one by its number.
How it works
Create with new int[5] (filled with zeros, null or false) or a literal {1, 2, 3}. a.length is the size (a field, not a method). a[a.length] throws ArrayIndexOutOfBoundsException.
System.out.println(a) prints a meaningless [I@1b6d3586. Use Arrays.toString(a) and, for nested arrays, Arrays.deepToString.
Arrays.sort(a), Arrays.fill(a, 0), Arrays.copyOf(a, n), Arrays.equals(a, b) (contents) and Arrays.asList(1, 2, 3) (a fixed-size list view) are the ones you use daily.
A two-dimensional array is an array of arrays: int[][] grid = new int[3][4]; and rows can have different lengths.
Going deeper
a == b and a.equals(b) compare identity, not contents. Use Arrays.equals or Arrays.deepEquals.
Arrays are covariant and checked at runtime: Object[] o = new String[1]; o[0] = 1; compiles but throws ArrayStoreException. Generic collections avoid this.
Arrays.asList(array) of a primitive array gives a one-element list of the array. It also does not support add or remove; wrap it in new ArrayList<>(...) if you need that.
Use an array when the size is known and performance matters (primitives, no boxing); otherwise use List.
Under the hood
An array is an object with a header, a length and the elements laid out contiguously, which makes iteration very cache friendly. Bounds are checked on every access, but the JIT removes the checks where it can prove they are safe.
Common mistakes
- Printing an array directly.
- Comparing arrays with
==orequals. - Off-by-one errors with
<=in the loop condition. - Expecting to resize an array.
How Unravel recognises it
When you paste code, Unravel looks for these signs of arrays and the arrays utilities. Spotting them yourself is a good habit when you read unfamiliar code:
- Method calls:
Arrays.sort()Arrays.fill()Arrays.asList()Arrays.toString()Arrays.deepToString()Arrays.equals()Arrays.stream()Arrays.setAll() - Language constructs:
arrayarray-init
Part of a learning path
- Text, numbers and arrays ยท previous: Random numbers and UUIDs ยท next: Bitwise operators and flags
Related concepts
Builds on this
Frequently asked questions
What is Arrays and the Arrays utilities?
An array is a fixed-size row of values of one type, indexed from 0: int[] a = new int[5]. The Arrays class sorts, fills, copies, compares and prints them.
What are the most common mistakes with Arrays and the Arrays utilities?
Printing an array directly. Comparing arrays with == or equals. Off-by-one errors with <= in the loop condition. Expecting to resize an array.
How can I see Arrays and the Arrays utilities in real code?
Paste your own Java into Unravel and it will point out where arrays and the arrays utilities appears, explain how it works at the detail level you choose, and draw the flow of the code around it.