Java Arrays Methods

About This Index
This page is the Java Arrays Methods index. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example is a complete Java class with a main method and fixed data, so the output shown is the output you get.
Learn what the Arrays class is on the Arrays hub. Use this page when you need a specific method such as toString(), sort(), or copyOf(). The examples compile on Java 8 and later, except on the three Java 9 pages listed below.
You always write Arrays.methodName(array, ...); the class cannot be instantiated. Arrays themselves keep a fixed length, so methods that "grow" or "shrink" an array, such as copyOf(), return a new array. compare(), compareUnsigned(), and mismatch() need Java 9 or newer.
How to Call an Arrays Method
Import java.util.Arrays once, then pass your array as the first argument:
import java.util.Arrays;
int[] nums = {5, 3, 9, 1};
Arrays.sort(nums); // nums is now 1, 3, 5, 9
System.out.println(Arrays.toString(nums)); // [1, 3, 5, 9]
int pos = Arrays.binarySearch(nums, 5); // 2
int[] bigger = Arrays.copyOf(nums, 6); // [1, 3, 5, 9, 0, 0]
Arrays.fill(bigger, 4, 6, 7); // [1, 3, 5, 9, 7, 7] Java Arrays Methods Table
All method guides in this Java tutorial series (21 tutorials), grouped by purpose.
Print and convert
3 methodsTurn an array into readable text, including nested arrays, or view it as a fixed-size List.
| Method | Description | Tutorial |
|---|---|---|
toString() | Print a one-dimensional array as readable text. | Try it |
deepToString() | Print nested arrays such as int[][] as readable text. | Try it |
asList() | View an array as a fixed-size List backed by the array. | Try it |
Copy
2 methodsMake a new array from an existing one: a longer or shorter copy, or a slice between two indexes.
| Method | Description | Tutorial |
|---|---|---|
copyOf() | Copy an array into a new one that is longer or shorter. | Try it |
copyOfRange() | Copy a slice of an array, from one index up to another. | Try it |
Fill and set
3 methodsPut one value into every slot (or a range), or compute each element from its index.
Sort and search
3 methodsSort an array in place, with or without the Fork/Join pool, and find a value in a sorted array.
| Method | Description | Tutorial |
|---|---|---|
sort() | Sort an array in place, whole or by range, with an optional Comparator. | Try it |
parallelSort() | Sort an array in place, using several threads for large arrays. | Try it |
binarySearch() | Find a value in a sorted array quickly, or where it would go. | Try it |
Compare
5 methodsCheck whether two arrays hold the same elements, order them, or find where they first differ. compare, compareUnsigned, and mismatch need Java 9.
| Method | Description | Tutorial |
|---|---|---|
equals() | Check whether two arrays have the same length and elements. | Try it |
deepEquals() | Compare nested arrays such as int[][] by their contents. | Try it |
compare() | Order two arrays lexicographically: negative, zero, or positive (Java 9). | Try it |
compareUnsigned() | Compare integer arrays as unsigned numbers (Java 9). | Try it |
mismatch() | Find the first index where two arrays differ, or -1 (Java 9). | Try it |
Hash codes
2 methodsCompute a hash code from the contents of an array, for flat or nested arrays.
| Method | Description | Tutorial |
|---|---|---|
hashCode() | Compute a hash code from the contents of an array. | Try it |
deepHashCode() | Compute a content-based hash code for nested arrays. | Try it |
Cumulative operations
1 methodReplace every element with a running result such as a running total or running maximum.
| Method | Description | Tutorial |
|---|---|---|
parallelPrefix() | Replace each element with a running total (or running max, product, ...). | Try it |
Iterate and stream
2 methodsWalk the elements with a Spliterator or process them with the Stream API.
| Method | Description | Tutorial |
|---|---|---|
spliterator() | Get a Spliterator over an array or a range of it. | Try it |
stream() | Process an array with the Stream API: sum, filter, map, collect. | Try it |
Frequently Asked Questions
Start with toString()
Print an array as readable text instead of a type code and hash.
12 people found this page helpful
