java.util.Arrays Methods
Arrays is a utility class that provides static methods for working with arrays.
Import it first:
import java.util.Arrays;
1. Sorting
sort(array)
Sorts the entire array in ascending order.
int[] arr = {5, 2, 8, 1};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
Output:
[1, 2, 5, 8]
sort(array, fromIndex, toIndex)
Sorts only a specific range. toIndex is exclusive.
int[] arr = {5, 4, 3, 2, 1};
Arrays.sort(arr, 1, 4); // from index 1 to 3 ( 4th does not count )
System.out.println(Arrays.toString(arr));
Output:
[5, 2, 3, 4, 1]
2. Filling
fill(array, value)
Fills the entire array with one value.
int[] arr = new int[5];
Arrays.fill(arr, 10);
System.out.println(Arrays.toString(arr));
Output:
[10, 10, 10, 10, 10]
fill(array, fromIndex, toIndex, value)
Fills only a specific range.
int[] arr = {1, 2, 3, 4, 5};
Arrays.fill(arr, 1, 4, 10);
System.out.println(Arrays.toString(arr));
Output:
[1, 10, 10, 10, 5]
3. Copying
copyOf(array, newLength)
Creates a new array with the specified length.
int[] arr = {1, 2, 3};
int[] copy = Arrays.copyOf(arr, 5);
System.out.println(Arrays.toString(copy));
Output:
[1, 2, 3, 0, 0]
copyOfRange(array, from, to)
Copies a specific range. to is exclusive.
int[] arr = {10, 20, 30, 40, 50};
int[] copy = Arrays.copyOfRange(arr, 1, 4);
System.out.println(Arrays.toString(copy));
Output:
[20, 30, 40]
4. Comparing Arrays
equals(array1, array2)
Checks whether two 1D arrays contain the same elements.
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
System.out.println(Arrays.equals(a, b));
Output:
true
deepEquals(array1, array2)
Compares nested/multidimensional arrays.
int[][] a = {{1, 2}, {3, 4}};
int[][] b = {{1, 2}, {3, 4}};
System.out.println(Arrays.deepEquals(a, b));
Output:
true
compare(array1, array2)
Compares arrays lexicographically.
int[] a = {1, 2, 3};
int[] b = {1, 2, 4};
System.out.println(Arrays.compare(a, b));
Output:
-1
Meaning a comes before b.
mismatch(array1, array2)
Returns the index of the first difference.
int[] a = {10, 20, 30};
int[] b = {10, 25, 30};
System.out.println(Arrays.mismatch(a, b));
Output:
1
Index 1 is where the arrays first differ.
5. Searching
binarySearch(array, key)
Searches a sorted array.
int[] arr = {10, 20, 30, 40, 50};
int index = Arrays.binarySearch(arr, 30);
System.out.println(index);
Output:
2
The array should be sorted before using
binarySearch().
binarySearch(array, from, to, key)
Searches only within a specific range.
int[] arr = {10, 20, 30, 40, 50};
int index = Arrays.binarySearch(arr, 1, 4, 30);
System.out.println(index);
Output:
2
6. Converting Arrays to String
toString(array)
Converts a 1D array into a readable string.
int[] arr = {1, 2, 3};
System.out.println(Arrays.toString(arr));
Output:
[1, 2, 3]
deepToString(array)
Converts a multidimensional array into a readable string.
int[][] arr = {{1, 2}, {3, 4}};
System.out.println(Arrays.deepToString(arr));
Output:
[[1, 2], [3, 4]]
7. Array to List
asList(array)
Converts an object array into a fixed-size List.
String[] arr = {"A", "B", "C"};
var list = Arrays.asList(arr);
System.out.println(list);
Output:
[A, B, C]
The returned list has a fixed size, so you cannot
add()orremove()elements.
8. Streams
stream(array)
Creates a Stream from an array.
int[] arr = {1, 2, 3, 4, 5};
Arrays.stream(arr)
.forEachprintln;
Output:
1
2
3
4
5
You can also perform operations:
int[] arr = {1, 2, 3, 4, 5};
int sum = Arrays.stream(arr).sum();
System.out.println(sum);
Output:
15
9. Setting Elements
setAll()
Sets each element using its index.
int[] arr = new int[5];
Arrays.setAll(arr, i -> i * 2);
System.out.println(Arrays.toString(arr));
Output:
[0, 2, 4, 6, 8]
Here i is the index.
parallelSetAll()
Similar to setAll(), but can perform the operation in parallel.
int[] arr = new int[5];
Arrays.parallelSetAll(arr, i -> i * 2);
System.out.println(Arrays.toString(arr));
Output:
[0, 2, 4, 6, 8]
10. Parallel Operations
parallelSort()
Sorts an array using a parallel sorting algorithm.
int[] arr = {5, 2, 8, 1, 3};
Arrays.parallelSort(arr);
System.out.println(Arrays.toString(arr));
Output:
[1, 2, 3, 5, 8]
For normal/smaller arrays, sort() is usually sufficient.
parallelPrefix()
Performs a cumulative operation on the array.
int[] arr = {1, 2, 3, 4};
Arrays.parallelPrefix(arr, (a, b) -> a + b);
System.out.println(Arrays.toString(arr));
Output:
[1, 3, 6, 10]
It calculates:
1
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
Quick Revision
| Method | What it does |
|---|---|
sort() |
Sorts array |
fill() |
Fills array with a value |
copyOf() |
Copies array with new length |
copyOfRange() |
Copies a range |
equals() |
Compares 1D arrays |
deepEquals() |
Compares multidimensional arrays |
compare() |
Lexicographically compares arrays |
mismatch() |
Finds first different index |
binarySearch() |
Searches sorted array |
toString() |
Array → readable String |
deepToString() |
Multidimensional array → String |
asList() |
Array → fixed-size List |
stream() |
Array → Stream |
setAll() |
Sets elements using index |
parallelSetAll() |
Parallel version of setAll() |
parallelSort() |
Parallel sorting |
parallelPrefix() |
Cumulative operation |
Most important for DSA
If you're learning Java for DSA, focus first on:
sort()
binarySearch()
copyOf()
copyOfRange()
equals()
toString()
fill()
These are the ones you'll encounter most frequently.