Last Updated: 28 May, 2022
Arrays is a utility class in Java, It was introduced in JDK 1.2 version and is available in java.util package. Arrays class is a member of the Java Collections Framework.
Arrays class provides various methods for manipulating arrays such as sorting, searching and comparing, etc. All the methods of the Arrays class are static so we can access any method with the Arrays class name, It also contains Object class methods.
The methods of the Arrays class throw a NullPointerException
if the specified array reference is null.
Declaration of Arrays Class
public class Arrays extends Object
Arrays Class provides following Methods:
Method | Description |
---|---|
List<T> asList(T[] arr) | Returns a fixed-size list backed by the specified Arrays. |
int binarySearch(T[] arr, element) | Searches for the specified element in the array with the help of Binary Search algorithm, and returns the position. |
int binarySearch(T[] arr, int fromIndex, int toIndex, T key, Comparator c) | Searches a range of the specified array for the specified object using the binary search algorithm. |
T[] copyOf(T[] originalArr, int newLength) | Copies the specified array, truncating or padding with the default value (if necessary) so the copy has the specified length. |
T[] copyOfRange(T[] originalArr, int fromIndex, int endIndex) | Copies the specified range of the specified array into a new Arrays. |
boolean equals(T[] arr1, T[] arr2) | Returns true if the two specified arrays of booleans are equal to one another, otherwise retruns false. |
boolean deepEquals(T[] arr1, T[] arr2) | Returns true if the two specified arrays of booleans are deeply equal to one another, otherwise retruns false (it compares including nested arrays). |
int hashCode(T[] arr) | Returns the hash code for the specified array. |
int deepHashCode(T[] arr) | Returns the hash code for the specified array including nested arrays. |
String toString(T[] arr) | Returns a string representation of the contents of the specified array. |
String deepToString(T[] arr) | Returns a string representation of the contents of the specified array including nested arrays. |
void fill(T[] arr, T value) | Assigns the specified value to each element of the specified array. |
void fill(T[] arr, int fromIndex, int toIndex, T value) | Assigns the specified value to each element of the specified range of the specified array. The range to be filled extends from fromIndex, inclusive, to toIndex, exclusive. |
void parallelPrefix(T[] arr, BinaryOperator o) | Cumulates, in parallel, each element of the given array in place, using the supplied function. |
void setAll(T[] arr, FunctionGenerator) | Sets all elements of the specified array, using the provided generator function to compute each element. |
void parallelSetAll(T[] arr, FunctionGenerator) | Sets all elements of the specified array, in parallel, using the provided generator function to compute each element. |
void sort(T[] arr) | Sorts the specified array into ascending order. |
void parallelSort(T[] arr) | Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. |
Of<T> spliterator(T[] arr) | Returns a Spliterator.Of<T> covering all of the specified array. |
Stream<T> stream(T[] arr) | Returns a sequential Stream with the specified array as its source. |
Example 1 : Using Arrays Class methods with Array
Output
List elements: [Red, Green, Yellow, Blue, Gray]
Index No: 2
Blue
Gray
Green
Red
Yellow
Example 2 : Comparing two arrays using Arrays class methods
Output
Arrays.compare(): -1
Arrays.compareUnsigned: -1
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
Why Arrays class is needed in Java Programming?
Arrays is a utility class in Java, It provides various methods for manipulating arrays such as sorting, searching, comparing, etc. but without these methods, suppose if we have to perform any operation such as sorting the array then we need to write the code for sorting which will increase the development task.