Arrays class in java
In Java, the Arrays class is a built-in class that provides static methods to manipulate and work with arrays. The Arrays class contains various utility methods for performing common operations on arrays, such as sorting, searching, filling, and comparing.
Some of the commonly used methods of the Arrays class are:
sort(): This method sorts the elements of an array in ascending order.
binarySearch(): This method searches for a specific element in a sorted array using the binary search algorithm.
fill(): This method assigns a specified value to each element of an array.
copyOf(): This method creates a new array and copies a specified number of elements from the original array.
equals(): This method compares two arrays to determine if they are equal.
toString(): This method returns a string representation of an array.
Here's an example code snippet demonstrating the usage of some of the methods of the Arrays class:
import java.util.Arrays;
public class ArraysDemo {
public static void main(String[] args) {
int[] arr = { 10, 20, 30, 40, 50 };
// Sorting the array
Arrays.sort(arr);
System.out.println("Sorted array: " + Arrays.toString(arr));
// Searching for an element in the array
int index = Arrays.binarySearch(arr, 30);
System.out.println("Index of 30 in the sorted array: " + index);
// Filling the array with a specific value
int[] newArr = new int[5];
Arrays.fill(newArr, 100);
System.out.println("New array: " + Arrays.toString(newArr));
}
}
Sorted array: [10, 20, 30, 40, 50]
Index of 30 in the sorted array: 2
New array: [100, 100, 100, 100, 100]
java.util.Arrays is a class in java programming language. Inside of this class exists 155 public methods. Declaration of this class as follows.
public class java.util.Arrays
Arrays public method
There are following useful methods which is define the inside of java.util.Arrays class.
Method | Description |
---|---|
static |
It returns a fixed-size list backed by the specified array. |
static int binarySearch(byte[] a, byte key) | It searches the specified array of bytes for the specified value using the binary search algorithm. |
static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key) | It searches a range of the specified array of bytes for the specified value using the binary search algorithm. |
static int binarySearch(char[] a, char key) | It searches the specified array of chars for the specified value using the binary search algorithm. |
static int binarySearch(char[] a, int fromIndex, int toIndex, char key) | It searches a range of the specified array of chars for the specified value using the binary search algorithm. |
static int binarySearch(double[] a, double key) | It searches the specified array of doubles for the specified value using the binary search algorithm. |
static int binarySearch(double[] a, int fromIndex, int toIndex, double key) | It searches a range of the specified array of doubles for the specified value using the binary search algorithm. |
static int binarySearch(float[] a, float key) | It searches the specified array of floats for the specified value using the binary search algorithm. |
static int binarySearch(float[] a, int fromIndex, int toIndex, float key) | It searches a range of the specified array of floats for the specified value using the binary search algorithm. |
static int binarySearch(int[] a, int key) | It searches the specified array of ints for the specified value using the binary search algorithm. |
static int binarySearch(int[] a, int fromIndex, int toIndex, int key) | It searches a range of the specified array of ints for the specified value using the binary search algorithm. |
static int binarySearch(long[] a, int fromIndex, int toIndex, long key) | It searches a range of the specified array of longs for the specified value using the binary search algorithm. |
static int binarySearch(long[] a, long key) | It searches the specified array of longs for the specified value using the binary search algorithm. |
static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key) | It searches a range of the specified array for the specified object using the binary search algorithm. |
static int binarySearch(Object[] a, Object key) | It searches the specified array for the specified object using the binary search algorithm. |
static int binarySearch(short[] a, int fromIndex, int toIndex, short key) | It searches a range of the specified array of shorts for the specified value using the binary search algorithm. |
static int binarySearch(short[] a, short key) | It searches the specified array of shorts for the specified value using the binary search algorithm. |
static |
It searches a range of the specified array for the specified object using the binary search algorithm. |
static |
It searches the specified array for the specified object using the binary search algorithm. |
static boolean[] copyOf(boolean[] original, int newLength) | It copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length. |
static byte[] copyOf(byte[] original, int newLength) | It copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. |
static char[] copyOf(char[] original, int newLength) | It copies the specified array, truncating or padding with null characters (if necessary) so the copy has the specified length. |
static double[] copyOf(double[] original, int newLength) | It copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. |
static float[] copyOf(float[] original, int newLength) | It copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. |
static int[] copyOf(int[] original, int newLength) | It copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. |
static long[] copyOf(long[] original, int newLength) | It copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. |
static short[] copyOf(short[] original, int newLength) | It copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. |
static |
It copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. |
static |
It copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. |
static boolean[] copyOfRange(boolean[] original, int from, int to) | It copies the specified range of the specified array into a new array. |
static byte[] copyOfRange(byte[] original, int from, int to) | It copies the specified range of the specified array into a new array. |
static char[] copyOfRange(char[] original, int from, int to) | It copies the specified range of the specified array into a new array. |
static double[] copyOfRange(double[] original, int from, int to) | It copies the specified range of the specified array into a new array. |
static float[] copyOfRange(float[] original, int from, int to) | It copies the specified range of the specified array into a new array. |
static int[] copyOfRange(int[] original, int from, int to) | It copies the specified range of the specified array into a new array. |
static long[] copyOfRange(long[] original, int from, int to) | It copies the specified range of the specified array into a new array. |
static short[] copyOfRange(short[] original, int from, int to) | It copies the specified range of the specified array into a new array. |
static |
It copies the specified range of the specified array into a new array. |
static |
It copies the specified range of the specified array into a new array. |
static boolean deepEquals(Object[] a1, Object[] a2) | It returns true if the two specified arrays are deeply equal to one another. |
static int deepHashCode(Object[] a) | It returns a hash code based on the "deep contents" of the specified array. |
static String deepToString(Object[] a) | It returns a string representation of the "deep contents" of the specified array. |
static boolean equals(boolean[] a, boolean[] a2) | It returns true if the two specified arrays of booleans are equal to one another. |
static boolean equals(byte[] a, byte[] a2) | It returns true if the two specified arrays of bytes are equal to one another. |
static boolean equals(char[] a, char[] a2) | It returns true if the two specified arrays of chars are equal to one another. |
static boolean equals(double[] a, double[] a2) | It returns true if the two specified arrays of doubles are equal to one another. |
static boolean equals(float[] a, float[] a2) | It returns true if the two specified arrays of floats are equal to one another. |
static boolean equals(int[] a, int[] a2) | It returns true if the two specified arrays of ints are equal to one another. |
static boolean equals(long[] a, long[] a2) | It returns true if the two specified arrays of longs are equal to one another. |
static boolean equals(Object[] a, Object[] a2) | It returns true if the two specified arrays of Objects are equal to one another. |
static boolean equals(short[] a, short[] a2) | It returns true if the two specified arrays of shorts are equal to one another. |
static void fill(boolean[] a, boolean val) | It assigns the specified boolean value to each element of the specified array of booleans. |
static void fill(boolean[] a, int fromIndex, int toIndex, boolean val) | It assigns the specified boolean value to each element of the specified range of the specified array of booleans. |
static void fill(byte[] a, byte val) | It assigns the specified byte value to each element of the specified array of bytes. |
static void fill(byte[] a, int fromIndex, int toIndex, byte val) | It assigns the specified byte value to each element of the specified range of the specified array of bytes. |
static void fill(char[] a, char val) | It assigns the specified char value to each element of the specified array of chars. |
static void fill(char[] a, int fromIndex, int toIndex, char val) | It assigns the specified char value to each element of the specified range of the specified array of chars. |
static void fill(double[] a, double val) | It assigns the specified double value to each element of the specified array of doubles. |
static void fill(double[] a, int fromIndex, int toIndex, double val) | It assigns the specified double value to each element of the specified range of the specified array of doubles. |
static void fill(float[] a, float val) | It assigns the specified float value to each element of the specified array of floats. |
static void fill(float[] a, int fromIndex, int toIndex, float val) | It assigns the specified float value to each element of the specified range of the specified array of floats. |
static void fill(int[] a, int val) | It assigns the specified int value to each element of the specified array of ints. |
static void fill(int[] a, int fromIndex, int toIndex, int val) | It assigns the specified int value to each element of the specified range of the specified array of ints. |
static void fill(long[] a, int fromIndex, int toIndex, long val) | It assigns the specified long value to each element of the specified range of the specified array of longs. |
static void fill(long[] a, long val) | It assigns the specified long value to each element of the specified array of longs. |
static void fill(Object[] a, int fromIndex, int toIndex, Object val) | It assigns the specified Object reference to each element of the specified range of the specified array of Objects. |
static void fill(Object[] a, Object val) | It assigns the specified Object reference to each element of the specified array of Objects. |
static void fill(short[] a, int fromIndex, int toIndex, short val) | It assigns the specified short value to each element of the specified range of the specified array of shorts. |
static void fill(short[] a, short val) | It assigns the specified short value to each element of the specified array of shorts. |
static int hashCode(boolean[] a) | It returns a hash code based on the contents of the specified array. |
static int hashCode(byte[] a) | It returns a hash code based on the contents of the specified array. |
static int hashCode(char[] a) | It returns a hash code based on the contents of the specified array. |
static int hashCode(double[] a) | It returns a hash code based on the contents of the specified array. |
static int hashCode(float[] a) | It returns a hash code based on the contents of the specified array. |
static int hashCode(int[] a) | It returns a hash code based on the contents of the specified array. |
static int hashCode(long[] a) | It returns a hash code based on the contents of the specified array. |
static int hashCode(Object[] a) | It returns a hash code based on the contents of the specified array. |
static int hashCode(short[] a) | It returns a hash code based on the contents of the specified array. |
static void parallelPrefix(double[] array, DoubleBinaryOperator op) | It cumulates, in parallel, each element of the given array in place, using the supplied function. |
static void parallelPrefix(double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op) | It performs for the given subrange of the array. |
static void parallelPrefix(int[] array, IntBinaryOperator op) | It cumulates, in parallel, each element of the given array in place, using the supplied function. |
static void parallelPrefix(int[] array, int fromIndex, int toIndex, IntBinaryOperator op) | It performs for the given subrange of the array. |
static void parallelPrefix(long[] array, int fromIndex, int toIndex, LongBinaryOperator op) | It performs for the given subrange of the array. |
static void parallelPrefix(long[] array, LongBinaryOperator op) | It cumulates, in parallel, each element of the given array in place, using the supplied function. |
static |
It cumulates, in parallel, each element of the given array in place, using the supplied function. |
static |
It performs for the given subrange of the array. |
static void parallelSetAll(double[] array, IntToDoubleFunction generator) | It set all elements of the specified array, in parallel, using the provided generator function to compute each element. |
static void parallelSetAll(int[] array, IntUnaryOperator generator) | It set all elements of the specified array, in parallel, using the provided generator function to compute each element. |
static void parallelSetAll(long[] array, IntToLongFunction generator) | It set all elements of the specified array, in parallel, using the provided generator function to compute each element. |
static |
It set all elements of the specified array, in parallel, using the provided generator function to compute each element. |
static void parallelSort(byte[] a) | It sorts the specified array into ascending numerical order. |
static void parallelSort(byte[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending numerical order. |
static void parallelSort(char[] a) | It sorts the specified array into ascending numerical order. |
static void parallelSort(char[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending numerical order. |
static void parallelSort(double[] a) | It sorts the specified array into ascending numerical order. |
static void parallelSort(double[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending numerical order. |
static void parallelSort(float[] a) | It sorts the specified array into ascending numerical order. |
static void parallelSort(float[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending numerical order. |
static void parallelSort(int[] a) | It sorts the specified array into ascending numerical order. |
static void parallelSort(int[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending numerical order. |
static void parallelSort(long[] a) | It sorts the specified array into ascending numerical order. |
static void parallelSort(long[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending numerical order. |
static void parallelSort(short[] a) | It sorts the specified array into ascending numerical order. |
static void parallelSort(short[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending numerical order. |
static |
It sorts the specified array of objects into ascending order, according to the natural ordering of its elements. |
static |
It sorts the specified array of objects according to the order induced by the specified comparator. |
static |
It sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements. |
static |
It sorts the specified range of the specified array of objects according to the order induced by the specified comparator. |
static void setAll(double[] array, IntToDoubleFunction generator) | It set all elements of the specified array, using the provided generator function to compute each element. |
static void setAll(int[] array, IntUnaryOperator generator) | It set all elements of the specified array, using the provided generator function to compute each element. |
static void setAll(long[] array, IntToLongFunction generator) | It set all elements of the specified array, using the provided generator function to compute each element. |
static |
It set all elements of the specified array, using the provided generator function to compute each element. |
static void sort(byte[] a) | It sorts the specified array into ascending numerical order. |
static void sort(byte[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending order. |
static void sort(char[] a) | It sorts the specified array into ascending numerical order. |
static void sort(char[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending order. |
static void sort(double[] a) | It sorts the specified array into ascending numerical order. |
static void sort(double[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending order. |
static void sort(float[] a) | It sorts the specified array into ascending numerical order. |
static void sort(float[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending order. |
static void sort(int[] a) | It sorts the specified array into ascending numerical order. |
static void sort(int[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending order. |
static void sort(long[] a) | It sorts the specified array into ascending numerical order. |
static void sort(long[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending order. |
static void sort(Object[] a) | It sorts the specified array of objects into ascending order, according to the natural ordering of its elements. |
static void sort(Object[] a, int fromIndex, int toIndex) | It sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements. |
static void sort(short[] a) | It sorts the specified array into ascending numerical order. |
static void sort(short[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending order. |
static |
It sorts the specified array of objects according to the order induced by the specified comparator. |
static |
It sorts the specified range of the specified array of objects according to the order induced by the specified comparator. |
static Spliterator.OfDouble spliterator(double[] array) | It returns a covering all of the specified array. |
static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) | It returns a covering the specified range of the specified array. |
static Spliterator.OfInt spliterator(int[] array) | It returns a covering all of the specified array. |
static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) | It returns a covering the specified range of the specified array. |
static Spliterator.OfLong spliterator(long[] array) | It returns a covering all of the specified array. |
static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) | It returns a covering the specified range of the specified array. |
static |
It returns a covering all of the specified array. |
static |
It returns a covering the specified range of the specified array. |
static DoubleStream stream(double[] array) | It returns a sequential with the specified array as its source. |
static DoubleStream stream(double[] array, int startInclusive, int endExclusive) | It returns a sequential with the specified range of the specified array as its source. |
static IntStream stream(int[] array) | It returns a sequential with the specified array as its source. |
static IntStream stream(int[] array, int startInclusive, int endExclusive) | It returns a sequential with the specified range of the specified array as its source. |
static LongStream stream(long[] array) | It returns a sequential with the specified array as its source. |
static LongStream stream(long[] array, int startInclusive, int endExclusive) | It returns a sequential with the specified range of the specified array as its source. |
static |
It returns a sequential with the specified array as its source. |
static |
It returns a sequential with the specified range of the specified array as its source. |
static String toString(boolean[] a) | It returns a string representation of the contents of the specified array. |
static String toString(byte[] a) | It returns a string representation of the contents of the specified array. |
static String toString(char[] a) | It returns a string representation of the contents of the specified array. |
static String toString(double[] a) | It returns a string representation of the contents of the specified array. |
static String toString(float[] a) | It returns a string representation of the contents of the specified array. |
static String toString(int[] a) | It returns a string representation of the contents of the specified array. |
static String toString(long[] a) | It returns a string representation of the contents of the specified array. |
static String toString(Object[] a) | It returns a string representation of the contents of the specified array. |
static String toString(short[] a) | It returns a string representation of the contents of the specified array. |
This reference is belong to javadoc
java.util.Arrays asList(T... a) method example
// asList(T... a) method in java.util.Arrays class
// Useful packages
import java.util.Arrays;
import java.util.List;
public class Example
{
public static void main(String[] args)
{
List < String > result = Arrays.asList("A", "Small", "Cap");
// Display result
System.out.println(result);
}
}

[A, Small, Cap]
asList() method are capable to deal with object[] array. When we pass an array of this method resultant variable is share the reference of given array internally.
// asList(Object[]) method in java.util.Arrays class
// Useful packages
import java.util.Arrays;
import java.util.List;
public class Example
{
public static void main(String[] args)
{
String[] arr = {
"A" , "Small" , "Code"
};
List < String > result = Arrays.asList(arr);
// Update value
arr[0] = "Add";
// Display result
System.out.println(result);
}
}

[Add, Small, Code]
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment