Arrays.equals() / Arrays.toString() / Arrays.asList()
Methods for comparing array contents, converting arrays to human-readable strings, and converting arrays to List. Useful for inspecting data during debugging and for working with collections.
Syntax
import java.util.Arrays; // Compares the contents of two one-dimensional arrays. Arrays.equals(a1, a2); // Compares the contents of multidimensional (nested) arrays. Arrays.deepEquals(a1, a2); // Converts a one-dimensional array to a string. Arrays.toString(array); // Converts a multidimensional array to a string. Arrays.deepToString(array); // Converts an array to a fixed-size List. Arrays.asList(T... a);
Method List
| Method | Description |
|---|---|
| Arrays.equals(a1, a2) | Returns true if two arrays have the same length and the same elements. Compares values, not references. |
| Arrays.deepEquals(a1, a2) | Recursively compares multidimensional arrays, including the contents of nested arrays. |
| Arrays.toString(array) | Converts all elements of an array to a string in a format like [1, 2, 3] and returns it. |
| Arrays.deepToString(array) | Recursively converts a multidimensional array to a string. A two-dimensional array is displayed as [[1, 2], [3, 4]]. |
| Arrays.asList(T... a) | Converts an array to a List. The list is fixed-size, so elements cannot be added or removed. |
Sample Code
import java.util.Arrays;
import java.util.ArrayList;
// Use Arrays.equals() to compare array contents.
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
int[] c = {1, 2, 4};
System.out.println(Arrays.equals(a, b)); // Prints: true
System.out.println(Arrays.equals(a, c)); // Prints: false
System.out.println(a == b); // Reference comparison, so prints: false
// Use Arrays.deepEquals() to compare two-dimensional arrays.
int[][] m1 = {{1, 2}, {3, 4}};
int[][] m2 = {{1, 2}, {3, 4}};
System.out.println(Arrays.deepEquals(m1, m2)); // Prints: true
// Use Arrays.toString() to display array contents for debugging.
int[] nums = {5, 1, 3, 2, 4};
System.out.println(Arrays.toString(nums)); // Prints: [5, 1, 3, 2, 4]
// Use Arrays.deepToString() to display a multidimensional array.
int[][] grid = {{1, 2, 3}, {4, 5, 6}};
System.out.println(Arrays.deepToString(grid)); // Prints: [[1, 2, 3], [4, 5, 6]]
// Use Arrays.asList() to convert to a List.
String[] arr = {"Java", "Python", "Swift"};
java.util.List<String> list = Arrays.asList(arr);
System.out.println(list); // Prints: [Java, Python, Swift]
// Convert to a resizable ArrayList.
ArrayList<String> mutableList = new ArrayList<>(Arrays.asList(arr));
mutableList.add("Go");
System.out.println(mutableList); // Prints: [Java, Python, Swift, Go]
Notes
Comparing arrays with == compares references, so even arrays with identical contents will return false. Always use Arrays.equals() to compare array contents.
Arrays.toString() is very handy for inspecting array contents during debugging. If you use Arrays.toString() on a multidimensional array (an array of arrays), the inner arrays are displayed as object references. Always use Arrays.deepToString() for multidimensional arrays.
To sort an array, use Arrays.sort(). To work with a List, see ArrayList's add() / get().
If you find any errors or copyright issues, please contact us.