The perfect place for easy learning...

Java Programming

×

Topics List


Arrays class in java





The java collection framework has a class Arrays that provides methods for creating dynamic array and perform various operations like search, asList, campare, etc.

The Arrays class in java is defined in the java.util package. All the methods defined by Arrays class are static methods.

The Arrays class in java has the following methods.

Method Description
List<T> asList(T[] arr) It returns a fixed-size list backed by the specified Arrays.
int binarySearch(T[] arr, element) It 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) It searches a range of the specified array for the specified object using the binary search algorithm.
T[] copyOf(T[] originalArr, int newLength) It 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) It copies the specified range of the specified array into a new Arrays.
boolean equals(T[] arr1, T[] arr2) It returns true if the two specified arrays of booleans are equal to one another, otherwise retruns false.
boolean deepEquals(T[] arr1, T[] arr2) It 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) It returns the hash code for the specified array.
int deepHashCode(T[] arr) It returns the hash code for the specified array including nested arrays.
String toString(T[] arr) It Returns a string representation of the contents of the specified array.
String deepToString(T[] arr) It Returns a string representation of the contents of the specified array including nested arrays.
void fill(T[] arr, T value) It assigns the specified value to each element of the specified array.
void fill(T[] arr, int fromIndex, int toIndex, T value) It 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) It Cumulates, in parallel, each element of the given array in place, using the supplied function.
void setAll(T[] arr, FunctionGenerator) It sets all elements of the specified array, using the provided generator function to compute each element.
void parallelSetAll(T[] arr, FunctionGenerator) It Sets all elements of the specified array, in parallel, using the provided generator function to compute each element.
void sort(T[] arr) It sorts the specified array into ascending order.
void parallelSort(T[] arr) It sorts the specified array of objects into ascending order, according to the natural ordering of its elements.
Of<T> spliterator(T[] arr) It returns a Spliterator.Of<T> covering all of the specified array.
Stream<T> stream(T[] arr) It returns a sequential Stream with the specified array as its source.

Let's consider an example program to illustrate methods of Arrays class.

Example
import java.util.*;

public class ArraysClassExample {

	public static void main(String[] args) {

		int[] arr1 = {10, 3, 50, 7, 30, 66, 28, 54, 42};

		int[] arr2 = {67, 2, 54, 67, 13, 56, 98};
		
		System.out.print("Array1 => ");
		for(int i:arr1)
			System.out.print(i + ", ");
		System.out.print("\nArray2 => ");
		for(int i:arr2)
			System.out.print(i + ", ");
		System.out.println("\n-------------------------------------------");
		
		System.out.println("Array1 as List => " + Arrays.asList(arr1));
		
		System.out.println("Position of 30 in Array1 => " + Arrays.binarySearch(arr1, 30));
		
		System.out.println("equity of array1 and array2 => " + Arrays.equals(arr1, arr2));
		
		System.out.println("Hash code of Array1 => " + Arrays.hashCode(arr1));
		
		Arrays.fill(arr1, 15);
		System.out.print("fill Array1 with 15 => ");
		for(int i:arr1)
			System.out.print(i + ", ");

		Arrays.sort(arr2);
		System.out.print("\nArray2 in sorted order => ");
		for(int i:arr2)
			System.out.print(i + ", ");
	}
}

When we execute the above code, it produce the following output.

Arrays class in java