Java Arrays Methods Descriptions Examples
Java Arrays Methods Descriptions Examples
1. toString(array)
- Description: Returns a string representation of the array.
- Example:
int[] arr = {1, 2, 3};
String result = Arrays.toString(arr); // "[1, 2, 3]"
2. sort(array)
- Description: Sorts the specified array into ascending order.
- Example:
Arrays.sort(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 = Arrays.copyOf(arr, 5);
5. fill(array, value)
- Description: Fills the array with the specified value.
- Example:
Arrays.fill(arr, 0); // All elements become 0
6. equals(arr1, arr2)
- Description: Compares two arrays for equality.
- Example:
boolean isEqual = Arrays.equals(arr1, arr2);
7. deepEquals(arr1, arr2)
- Description: Compares nested arrays for equality.
- Example:
boolean isDeepEqual = Arrays.deepEquals(nested1, nested2);
8. binarySearch(array, key)
- Description: Searches the array for the specified value (must be sorted).
- Example:
int index = Arrays.binarySearch(arr, 3);
9. mismatch(arr1, arr2)
- Description: Finds and returns the index of the first mismatch between two arrays.
- Example:
int mismatchIndex = Arrays.mismatch(arr1, arr2);
10. parallelSort(array)
- Description: Sorts the array using parallel sorting.
- Example:
Arrays.parallelSort(arr);
11. stream(array)
- Description: Returns a sequential stream from the array.
- Example:
Arrays.stream(arr).forEach(System.out::println);