Java Arrays Class Methods - Descriptions & Examples
Java Arrays Class Methods - Descriptions & Examples
1. toString(array)
- Description: Returns a string representation of the array.
- Example:
int[] arr = {1, 2, 3};
String result = [Link](arr); // "[1, 2, 3]"
2. sort(array)
- Description: Sorts the specified array into ascending order.
- Example:
[Link](arr); // arr becomes [1, 2, 3]
3. copyOf(array, newLength)
- Description: Copies the original array to a new array of the specified length.
- Example:
int[] newArr = [Link](arr, 5);
4. copyOfRange(array, from, to)
- Description: Copies a specified range from the original array.
- Example:
int[] subArr = [Link](arr, 1, 3);
5. fill(array, value)
- Description: Fills the array with the specified value.
- Example:
[Link](arr, 0); // All elements become 0
6. equals(arr1, arr2)
- Description: Compares two arrays for equality.
- Example:
boolean isEqual = [Link](arr1, arr2);
7. deepEquals(arr1, arr2)
- Description: Compares nested arrays for equality.
- Example:
boolean isDeepEqual = [Link](nested1, nested2);
8. binarySearch(array, key)
- Description: Searches the array for the specified value (must be sorted).
- Example:
int index = [Link](arr, 3);
9. mismatch(arr1, arr2)
- Description: Finds and returns the index of the first mismatch between two arrays.
- Example:
int mismatchIndex = [Link](arr1, arr2);
10. parallelSort(array)
- Description: Sorts the array using parallel sorting.
- Example:
[Link](arr);
11. stream(array)
- Description: Returns a sequential stream from the array.
- Example:
[Link](arr).forEach([Link]::println);
12. setAll(array, IntUnaryOperator)
- Description: Sets each element using a generator function.
- Example:
[Link](arr, i -> i * i);