Javatpoint Logo
Javatpoint Logo

Java Array Methods

Java's Arrays class in the java.util package offers a range of static methods facilitating operations on arrays. It provides functionality to fill, sort, search, and more. The methods enhance array manipulation, contributing to cleaner and more efficient code.

Let's examine the operations provided by the Arrays class by reviewing its methods presented below in a tabular format.

Method Description
asList() The asList() method in Java is a static method defined in the Arrays class that converts an array into a fixed-size list. The method returns a list with the original array as its backing. It indicates that any changes to the list will be reflected in the original array as well.
binarySearch() Utilizes the binary search algorithm to efficiently locate a specified value within a sorted array. Returns the index of the target value if found, or a negative value indicating the insertion point if not found.
binarySearch(array, fromIndex, toIndex, key, Comparator) Performs a binary search within a specified range of a sorted array using a custom comparator. The fromIndex and toIndex parameters define the inclusive range of the search.
compare(array 1, array 2) The compare() method in Java is a static method defined in the Arrays class that compares two arrays of the same data type lexicographically.
copyOf(originalArray, newLength) The copyOf method in java.util.Arrays duplicates an array, adjusting its length to a specified size by truncating or padding with default values as necessary.
copyOfRange(originalArray, fromIndex, endIndex) The copyOfRange(originalArray, fromIndex, toIndex) method in java.util.Arrays creates a new array by copying elements from the original array within the specified range, starting from fromIndex (inclusive) and ending at toIndex (exclusive).
deepEquals(Object[] a1, Object[] a2) The deepEquals() method in Java is a static method defined in the Arrays class that checks whether two arrays of objects are deeply equal to one another. If two arrays are deeply identical, they are either null or have the same number of elements and all corresponding pairs of elements remain deeply equal.
deepHashCode(Object[] a) Java computes a hash code based on the "deep contents" of the specified array. It suggests that the hashCode() function considers the hash codes of individual elements in the array, encompassing the hash codes of any nested arrays contained within those elements.
deepToString(Object[] a) The deepToString(Object[] a) method in java.util.Arrays generates a string representation of the specified array.
equals(array1, array2) The Arrays.equals() method in Java checks if two arrays, whether containing objects or primitive types, are equal. It looks at each pair of elements in the arrays, considering them equal only if the arrays have the same length and each pair of elements matches.
fill(originalArray, fillValue) The fill value is assigned to each index of this array.
hashCode(originalArray) The hashCode(originalArray) method in java.util.Arrays generates a hash code for the specified array.
mismatch(array1, array2) The Arrays.mismatch() method in Java finds and returns the index of the first mismatch between two Object arrays. If there is no difference identified between the elements being compared, the method returns -1.
parallelPrefix(originalArray, fromIndex, endIndex, functionalOperator) The parallelPrefix() method in Java is a static method defined in the Arrays class that performs a parallel prefix operation on a specified range of an array using a custom functional operator.
parallelPrefix(originalArray, operator) The parallelPrefix() method in Java is a static method defined in the Arrays class that performs a parallel prefix operation on the entire array using a custom functional operator.
parallelSetAll(originalArray, functionalGenerator) The parallelSetAll() method in Java is a static method defined in the Arrays class that sets all elements of an array in parallel using a provided generator function to compute each element.
parallelSort(originalArray) The parallelSort() method in Java is a static method defined in the Arrays class that sorts an array in parallel using the merge sort algorithm.
setAll(originalArray, functionalGenerator) The setAll() method in Java is a static method defined in the Arrays class that sets all elements of an array in parallel using a provided generator function to compute each element.
sort(originalArray) The sort(originalArray) method in java.util.Arrays performs an ascending-order sort on the entire array (originalArray).
sort(originalArray, fromIndex, endIndex) The sort(originalArray, fromIndex, endIndex) method in Java is a static method defined in the Arrays class that sorts a specified range of an array in place using a ascending function.
sort(T[] a, int fromIndex, int toIndex, Comparator< super T> c) The sort(T[] a, int fromIndex, int toIndex, Comparator< super T> c) method in Java is a static method defined in the Arrays class that sorts a specified range of an array of objects according to the order induced by the specified comparator.
sort(T[] a, Comparator< super T> c) The sort(T[] a, Comparator< super T> c) method in Java is a static method defined in the Arrays class that sorts an entire array of objects according to the order induced by the specified comparator.
spliterator(originalArray) The spliterator(originalArray) method in Java is a static method defined in the Arrays class that returns a Spliterator for the specified array.
spliterator(originalArray, fromIndex, endIndex) The spliterator(originalArray, fromIndex, endIndex) method in Java is a static method defined in the Arrays class that returns a Spliterator for the specified range of the array.
stream(originalArray) The toString(originalArray) method in Java is a static method defined in the Arrays class that returns a string representation of the specified array.
toString(originalArray) The toString(originalArray) method in java.util.Arrays provides a string representation of the array's contents. The resulting string showcases the array elements enclosed in square brackets and separated by commas. Each element is converted to a string using String.valueOf(), making it a handy representation for display or debugging.

Implementation:

Example 1

In the following program, we have used the asList(), binarySearch(), compare(), and the compareUnsigned() method.

Filename: ArrayMethodsExample1.java

Output:

Original Array: [1, 4, 7, 10, 13]
Index of 7: 2
Index of 10 in range [1, 4]: 3
Comparison result between array1 and array2: 0
Unsigned comparison result between unsignedArray1 and unsignedArray2: 1

Example 2

In the following program, we have used the copyOf(), copyOfRange(), deepEquals(), deepHashCode(), and deepToString() method.

Filename: ArrayMethodsExample2.java

Output:

Copy of originalArray with new length: [1, 2, 3, 4, 5, 0, 0]
Copy of originalArray from index 1 to 4: [2, 3, 4]
Are array1 and array2 deepEquals? true
Deep hash code of array1: 30848
Deep string representation of array1: [[1, 2, 3]]

Example 3

In the following program, we have used the equals(), fill(), hashCode(), mismatch(), and parallelSort() method.

Filename: ArrayMethodsExample3.java

Output:

Are array1 and array2 equal? true
Filled array with value 7: [7, 7, 7, 7, 7]
Hash code of the array: 35309286
Mismatch index between mismatchArray1 and mismatchArray2: 3
Sorted array using parallelSort: [1, 2, 3, 4, 5]

Example 4

In the following program, we have used the various types of the sort() method and the spliterator method.

Filename: ArrayMethodsExample4.java

Output:

Sorted array using sort(): [1, 2, 3, 4, 5]
Partially sorted array from index 1 to 4: [5, 1, 3, 4, 2]
Sorted string array by length: [Apple, Banana, Orange, Grapes]
Sorted array in reverse order: [5, 4, 3, 2, 1]
Spliterator characteristics: 17488

Example 5

In the following program, we have used the spliterator(), stream(), and the toString() method.

Filename: ArrayMethodExample5.java

Output:

Spliterator characteristics: 17488
IntStream from originalArray: 
1
2
3
4
5
String representation of originalArray: [1, 2, 3, 4, 5]






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA