Common Java Array Operations
Perform common array operations in Java — length, copy, fill, search, and conversion to lists.
Arrays themselves don't have many methods. The array object exposes one field (length) and one method (clone()). Everything else — printing, filling, searching, comparing — lives on the java.util.Arrays utility class, which we'll cover in depth in the next chapter. This chapter is a tour of the day-to-day operations: what they do and which call to reach for.
Length
int[] data = {3, 1, 4, 1, 5, 9, 2, 6};
System.out.println(data.length); // 8Field, not method — no parentheses. length is fixed once the array is created.
Printing an array
System.out.println(arr) prints something like [I@1540e19d — the class name and hash. That's almost never what you want. Use Arrays.toString:
import java.util.Arrays;
int[] data = {3, 1, 4, 1, 5};
System.out.println(Arrays.toString(data)); // [3, 1, 4, 1, 5]For 2D arrays use Arrays.deepToString:
int[][] grid = {{1, 2}, {3, 4}};
System.out.println(Arrays.deepToString(grid)); // [[1, 2], [3, 4]]Filling
To set every slot to the same value:
int[] zeros = new int[5];
Arrays.fill(zeros, 7); // {7, 7, 7, 7, 7}A range form fills only [fromIndex, toIndex):
int[] data = new int[10];
Arrays.fill(data, 3, 7, 1); // ones in positions 3, 4, 5, 6Equality
== on arrays compares references, not contents. For element-by-element equality, use Arrays.equals:
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
System.out.println(a == b); // false (different objects)
System.out.println(Arrays.equals(a, b)); // trueFor nested arrays, use Arrays.deepEquals. For null-safe semantics, both helpers consider null == null to be true.
Searching
A linear scan with a classic loop is fine for small arrays or unknown order:
int[] data = {7, 3, 9, 1, 5};
int target = 9;
int found = -1;
for (int i = 0; i < data.length; i++) {
if (data[i] == target) { found = i; break; }
}For a sorted array, Arrays.binarySearch is O(log n):
int[] sorted = {1, 3, 5, 7, 9};
int idx = Arrays.binarySearch(sorted, 7); // 3If the value isn't present, binarySearch returns a negative number that encodes where it would go: -(insertionPoint) - 1. The array must be sorted in advance — otherwise the result is undefined.
Copying
To get a fresh array with the same contents:
int[] data = {1, 2, 3, 4, 5};
int[] copy = Arrays.copyOf(data, data.length);To resize while copying — pad with default values if longer, truncate if shorter:
int[] longer = Arrays.copyOf(data, 8); // {1, 2, 3, 4, 5, 0, 0, 0}
int[] shorter = Arrays.copyOf(data, 3); // {1, 2, 3}To copy a slice:
int[] middle = Arrays.copyOfRange(data, 1, 4); // {2, 3, 4}from is inclusive, to is exclusive — the usual Java half-open convention.
There's more depth on copying — including System.arraycopy and clone() — in the dedicated Copying arrays chapter.
Converting to a List
Arrays.asList(...) wraps an array (of references) as a fixed-size List:
String[] arr = {"a", "b", "c"};
List<String> list = Arrays.asList(arr);It's fixed-size: add/remove throw UnsupportedOperationException, but you can list.set(...). Note asList does not work with primitive arrays the way you'd hope — Arrays.asList(new int[]{1, 2, 3}) produces a List<int[]> of length 1, not a List<Integer>. For primitives, use streams:
import java.util.stream.IntStream;
int[] nums = {1, 2, 3};
List<Integer> boxed = IntStream.of(nums).boxed().toList();Hashing
Arrays.hashCode(arr) produces a content-based hash you can use in equals/hashCode implementations of a containing class:
int[] data = {1, 2, 3};
int h = Arrays.hashCode(data);For nested arrays, use Arrays.deepHashCode.
A worked example
What's next
The helpers you've just used — toString, fill, equals, copyOf, binarySearch, sort — all live on the same class: java.util.Arrays. The next chapter looks at the Arrays utility class systematically, including pieces we glossed over here.
Practice
What does System.out.println(arr) print for an int[]?