Java Arrays Class
In this page:
import java.util.Arrays;
Arrays.sort(array);
Arrays.toString(array);
Arrays.fill(array, value);
Printing Arrays with toString()
Printing an array directly with System.out.println(arr) shows something like [I@1b6d3586 — the array's type and memory hash, not its contents — because arrays don't override toString().
Arrays.toString(arr) from java.util.Arrays gives you the readable [1, 2, 3] form instead.
Example: Printing Arrays with toString()
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
System.out.println(arr); // e.g. [I@1b6d3586
System.out.println(Arrays.toString(arr)); // [1, 2, 3]
}
}
Login to try C/C++/Java/PHP code in the editor
Sorting Arrays
Arrays.sort() sorts a primitive array in place in ascending order using a tuned dual-pivot quicksort, and it also works on object arrays (like String[]) as long as the elements implement Comparable or you supply a Comparator.
Example: Sorting Arrays
// Import java.util.Arrays so it can be used by its short name
import java.util.Arrays;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare arr and set it to {5, 2, 8, 1}
int[] arr = {5, 2, 8, 1};
Arrays.sort(arr);
// Print a line to the console
System.out.println(Arrays.toString(arr));
}
}
Login to try C/C++/Java/PHP code in the editor
Searching with Binary Search
Arrays.binarySearch() finds an element's index in O(log n) time, but only works correctly if the array is already sorted — running it on an unsorted array gives an undefined, unreliable result rather than a clear error.
Example: Searching with Binary Search
// Import java.util.Arrays so it can be used by its short name
import java.util.Arrays;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare arr and set it to {1, 3, 5, 7, 9}
int[] arr = {1, 3, 5, 7, 9};
// Declare index and set it to Arrays.binarySearch(arr, 7)
int index = Arrays.binarySearch(arr, 7);
// Print a line to the console
System.out.println(index);
}
}
Login to try C/C++/Java/PHP code in the editor
Filling Arrays
Arrays.fill() overwrites every element (or a specified range) with the same value in one call, which is convenient for resetting a counting array or initializing a buffer to a sentinel value like -1 before real data is written in.
Example: Filling Arrays
// Import java.util.Arrays so it can be used by its short name
import java.util.Arrays;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare arr and set it to new int[5]
int[] arr = new int[5];
Arrays.fill(arr, -1);
// Print a line to the console
System.out.println(Arrays.toString(arr));
}
}
Login to try C/C++/Java/PHP code in the editor
Copying Arrays
Arrays.copyOf() creates a new array with the same or a different length, copying over as many original elements as will fit and padding the rest with default values if the new array is larger.
This is one common way Java code implements a manual resize since arrays themselves can't grow.
Example: Copying Arrays
// Import java.util.Arrays so it can be used by its short name
import java.util.Arrays;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare original and set it to {1, 2, 3}
int[] original = {1, 2, 3};
// Declare copy and set it to Arrays.copyOf(original, 5)
int[] copy = Arrays.copyOf(original, 5);
// Print a line to the console
System.out.println(Arrays.toString(copy));
}
}
Login to try C/C++/Java/PHP code in the editor
- Printing an array directly with
System.out.println(arr), which shows something like[I@1b6d3586; useArrays.toString(arr). - Using
Arrays.binarySearchon an array that is not sorted, which gives wrong results. - Expecting
Arrays.asList()on a primitiveint[]to make a list of ints.
- Methods package reusable code, take parameters, return values, and can be overloaded, recursive, static, or use varargs.
- Arrays store several values of one type, including multi-dimensional arrays.
- The
Arraysclass provides helpful utilities for working with array data.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: