All Java Arrays methods
Here’s a complete Java array methods list with short descriptions — covering both the built-in property and all important methods from the java.util.Arrays utility class.
1️⃣ Core Array Property
| Method / Property | Description |
length (property) | Returns the number of elements in the array. |
2️⃣ java.util.Arrays Methods
| Method | Description |
sort(array) | Sorts the array in ascending order. |
sort(array, fromIndex, toIndex) | Sorts a specific range within the array. |
parallelSort(array) (Java 8+) | Sorts the array using parallel threads for better performance on large arrays. |
binarySearch(array, key) | Searches for the specified key in a sorted array; returns index if found, else negative value. |
binarySearch(array, fromIndex, toIndex, key) | Searches within a specific range of a sorted array. |
copyOf(original, newLength) | Copies the original array into a new array of given length. |
copyOfRange(original, from, to) | Copies a specific range from the original array into a new array. |
fill(array, value) | Fills the entire array with the specified value. |
fill(array, fromIndex, toIndex, value) | Fills a range of the array with the specified value. |
equals(array1, array2) | Returns true if both arrays contain the same elements in the same order. |
deepEquals(array1, array2) | Compares two multi-dimensional arrays for equality. |
toString(array) | Returns a string representation of the array. |
deepToString(array) | Returns a string representation of a multi-dimensional array. |
setAll(array, generator) (Java 8+) | Sets each element of the array using a generator function. |
parallelSetAll(array, generator) (Java 8+) | Similar to setAll but uses parallel computation. |
stream(array) (Java 8+) | Converts the array into a Stream for functional operations like map, filter. |
spliterator(array) (Java 8+) | Creates a Spliterator for use in parallel processing. |
If you want, I can prepare the same style table for Java String methods so both arrays and strings are in your interview-ready cheat sheet format. That way, you’ll have methods + descriptions for both.
You said:
with all exmpale
ChatGPT said:
Alright — I’ll give you a full Java Array Methods Cheat Sheet with all important methods + descriptions + working examples in one file.
This will include core array features and all major java.util.Arrays methods.
javaCopyEditimport java.util.Arrays;
import java.util.stream.IntStream;
public class AllArrayMethodsExample {
public static void main(String[] args) {
// 1️⃣ length (property)
int[] numbers = {5, 3, 9, 1, 4};
System.out.println("length: " + numbers.length); // 5
// 2️⃣ sort()
Arrays.sort(numbers);
System.out.println("sort(): " + Arrays.toString(numbers)); // [1, 3, 4, 5, 9]
// 3️⃣ sort(array, fromIndex, toIndex)
int[] partialSort = {10, 3, 8, 1, 7};
Arrays.sort(partialSort, 1, 4);
System.out.println("sort(from,to): " + Arrays.toString(partialSort)); // [10, 1, 3, 8, 7]
// 4️⃣ parallelSort()
int[] parallelArr = {9, 2, 7, 1, 6};
Arrays.parallelSort(parallelArr);
System.out.println("parallelSort(): " + Arrays.toString(parallelArr)); // [1, 2, 6, 7, 9]
// 5️⃣ binarySearch()
int index = Arrays.binarySearch(numbers, 4);
System.out.println("binarySearch(4): " + index); // 2
// 6️⃣ binarySearch(from,to,key)
int indexRange = Arrays.binarySearch(numbers, 1, 4, 5);
System.out.println("binarySearch(from,to,5): " + indexRange); // 3
// 7️⃣ copyOf()
int[] copy = Arrays.copyOf(numbers, numbers.length);
System.out.println("copyOf(): " + Arrays.toString(copy));
// 8️⃣ copyOfRange()
int[] rangeCopy = Arrays.copyOfRange(numbers, 1, 4);
System.out.println("copyOfRange(1,4): " + Arrays.toString(rangeCopy)); // [3, 4, 5]
// 9️⃣ fill()
int[] filled = new int[5];
Arrays.fill(filled, 7);
System.out.println("fill(): " + Arrays.toString(filled)); // [7, 7, 7, 7, 7]
// 🔟 fill(from,to,value)
Arrays.fill(filled, 1, 4, 99);
System.out.println("fill(from,to,value): " + Arrays.toString(filled)); // [7, 99, 99, 99, 7]
// 1️⃣1️⃣ equals()
System.out.println("equals(): " + Arrays.equals(numbers, copy)); // true
// 1️⃣2️⃣ deepEquals() for multi-dimensional arrays
int[][] arr2D1 = {{1, 2}, {3, 4}};
int[][] arr2D2 = {{1, 2}, {3, 4}};
System.out.println("deepEquals(): " + Arrays.deepEquals(arr2D1, arr2D2)); // true
// 1️⃣3️⃣ toString()
System.out.println("toString(): " + Arrays.toString(numbers)); // [1, 3, 4, 5, 9]
// 1️⃣4️⃣ deepToString()
System.out.println("deepToString(): " + Arrays.deepToString(arr2D1)); // [[1, 2], [3, 4]]
// 1️⃣5️⃣ setAll()
int[] squares = new int[5];
Arrays.setAll(squares, i -> i * i);
System.out.println("setAll(): " + Arrays.toString(squares)); // [0, 1, 4, 9, 16]
// 1️⃣6️⃣ parallelSetAll()
int[] cubes = new int[5];
Arrays.parallelSetAll(cubes, i -> i * i * i);
System.out.println("parallelSetAll(): " + Arrays.toString(cubes)); // [0, 1, 8, 27, 64]
// 1️⃣7️⃣ stream()
int sum = Arrays.stream(numbers).sum();
System.out.println("stream().sum(): " + sum); // 22
// 1️⃣8️⃣ spliterator()
System.out.print("spliterator(): ");
Arrays.spliterator(numbers).forEachRemaining(n -> System.out.print(n + " "));
System.out.println();
}
}
📋 Summary Table — Array Methods with Descriptions
| Method | Description |
length | Returns the number of elements in the array. |
sort(array) | Sorts in ascending order. |
sort(array, from, to) | Sorts a subrange of the array. |
parallelSort(array) | Parallel sort for large arrays. |
binarySearch(array, key) | Searches in sorted array. |
binarySearch(array, from, to, key) | Searches in a sorted subrange. |
copyOf(array, length) | Copies to a new array of given length. |
copyOfRange(array, from, to) | Copies a range into a new array. |
fill(array, value) | Fills entire array with a value. |
fill(array, from, to, value) | Fills a range with a value. |
equals(a1, a2) | Compares arrays element-by-element. |
deepEquals(a1, a2) | Compares multi-dimensional arrays deeply. |
toString(array) | Returns a string representation. |
deepToString(array) | String representation of multi-dimensional array. |
setAll(array, generator) | Sets each element using a lambda. |
parallelSetAll(array, generator) | Parallel version of setAll(). |
stream(array) | Converts array to a Stream. |
spliterator(array) | Creates a Spliterator for parallel processing. |