Java Array Class Tutorial – java.util.Arrays Class with Examples

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated March 10, 2024

This Tutorial Covers the Array Class in Java and the methods of java.util.arrays Class along with Detailed Description & Examples of Array Class methods:

The ‘Arrays’ class is a member of the ‘java.util’ package. This is a part of the Java Collections framework and provides methods to create, access and manipulate Java arrays dynamically.

All the methods provided by the Arrays class are static in nature and are methods of the ‘Object’ class. As the methods are static, they can be accessed using the class name itself.

=> Check ALL Java Tutorials Here.

Java _Arrays_ Class

Java Array Class

The Arrays class was introduced in Java 1.2 and the methods it contains are mostly used for manipulation of the array including searching, sorting, etc. The arrays class provides overloaded methods for almost all the data types.

The class hierarchy for Arrays class is shown below:

class hierarchy for Arrays

The Arrays class extends from Object class and its methods are methods of Object class.

The general syntax to access any method of Arrays class is:

Arrays.<method_name>;

In the upcoming section, we will list out the various methods provided by the Arrays class.

Java Arrays Methods

The following tables give an introduction to the various methods provided by the Arrays class. Here we have listed the main methods. Note that most of the methods are overloaded to provide support for all the primitive types.

We will list the prototype and description of each of the functions. Then in the following section, we will describe some of the important methods by providing programming examples.

Method NamePrototypeDescription
asListstatic< T> List<
T>asList(Object[] a)
Returns a list(fixed-size) from specified array
binarySearch
This method uses the binary search algorithm.
Shown in the next column are various overloads of the binarySearch method.
static int binarySearch(byte[] a, byte key)Searches for a key in a byte array
static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key)Searches the key across the specified range in a byte array
static int binarySearch(char[] a, char key)Searches a key in a character array
static int binarySearch(char[] a, int fromIndex, int toIndex, char key)Searches the key across the specified range in a character array
static int binarySearch(double[] a, double key)Searches a key in a double array
static int binarySearch(double[] a, int fromIndex, int toIndex, double key)Searches the key across the specified range in a double array
static int binarySearch(float[] a, float key)Searches a key in an array of floats
static int binarySearch(float[] a, int fromIndex, int toIndex, float key)Searches the key across the specified range in a floats array
static int binarySearch(int[] a, int key)Searches a key in an int array
static int binarySearch(int[] a, int fromIndex, int toIndex, int key)Searches the key across the specified range in an int array
static int binarySearch(long[] a, long key)Searches a key in long array
static int binarySearch(long[] a, int fromIndex, int toIndex, long key)Searches the key across the specified range in long array
static int binarySearch(Object[] a, Object key)Searches a key in an object array
static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key)Searches the key across the specified range in the object array
static int binarySearch(short[] a, short key)Searches a key in an array of shorts
static int binarySearch(short[] a, int fromIndex, int toIndex, short key)Searches the key across the specified range in an array of shorts
static < T> int binarySearch(T[] a, T key, Comparator< ? super T> c)Searches a key in an array of specified objects
static < T> int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator< ? super T> c)Searches the key across the specified range in array of objects
Method NamePrototypeDescription
copyOf

The method is used to copy the array with the specified length.

Next column lists the overloads of this method
static boolean[]copyOf(boolean[] original, int newLength)Copies the specified array. Truncates or appends values ‘false’ if necessary
static byte[]copyOf(byte[] original, int newLength)Copies the specified array. Truncates or appends zeros if necessary
static char[]copyOf(char[] original, int newLength)Copies the specified array. Truncates or appends null if necessary
static double[] copyOf(double[] original, int newLength)Copies the specified array. Truncates or appends zeros if necessary
static float[]copyOf(float[] original, int newLength)Copies the specified array. Truncates or appends zeros if necessary
static int[]copyOf(int[] original, int newLength)Copies the specified array. Truncates or appends zeros if necessary
static long[]copyOf(long[] original, int newLength)Copies the specified array. Truncates or appends zeros if necessary
static short[]copyOf(short[] original, int newLength)Copies the specified array. Truncates or appends zeros if necessary
static < T> T[] copyOf(T[] original, int newLength)Copies the specified array. Truncates or appends nulls if necessary
static T[]copyOf(U[] original, int newLength, Class< ? extends T[]>newType)Copies the specified array. Truncates or appends nulls if necessary
copyOfRange

This method is used to copy a specified range in the array.

The overloads for this method are given in the next column
static boolean[]copyOfRange(boolean[] original, int from, int to)Copies the array with specified range into a new array
static byte[]copyOfRange(byte[] original, int from, int to)Copies the array with specified range into a new array
static char[]copyOfRange(char[] original, int from, int to)Copies the array with specified range into a new array
static double[] copyOfRange(double[] original, int from, int to)Copies the array with specified range into a new array
static float[]copyOfRange(float[] original, int from, int to)Copies the array with specified range into a new array
static int[]copyOfRange(int[] original, int from, int to)Copies the array with specified range into a new array
static long[]copyOfRange(long[] original, int from, int to)Copies the array with specified range into a new array
static short[]copyOfRange(short[] original, int from, int to)Copies the array with specified range into a new array
static < T> T[] copyOfRange(T[] original, int from, int to)Copies the array with specified range into a new array
static < T,U> T[] copyOfRange(U[] original, int from, int to, Class< ? extends T[]>newType)Copies the array with specified range into a new array
Method NamePrototypeDescription
deepEqualsstatic boolean deepEquals(Object[] a1, Object[] a2)Checks if two specified arrays are deeply equal
deepHashCodestatic intdeepHashCode(Object[] a)Returns a hash code of the specified array
deepToStringstatic StringdeepToString(Object[] a)Returns the "deep contents" of the specified array in a string
Equals

Checks if two specified arrays are equal
static boolean equals(boolean[] a, boolean[] a2)Returns true if the two specified booleanarrays are equal.
static boolean equals(byte[] a, byte[] a2)Returns true if the two specified byte arrays are equal
static boolean equals(char[] a, char[] a2)Returns true if the two specified character arrays are equal.
static boolean equals(double[] a, double[] a2)Returns true if the two specified double arrays are equal.
static boolean equals(float[] a, float[] a2)Returns true if the two specified float arrays are equal.
static boolean equals(int[] a, int[] a2)Returns true if the two specified int arrays are equal.
static boolean equals(long[] a, long[] a2)Returns true if the two specified long arrays are equal.
static boolean equals(Object[] a, Object[] a2)Returns true if the two specified Object arrays are equal.
static boolean equals(short[] a, short[] a2)Returns true if the two specified short arrays are equal.
Method NamePrototypeDescription
fill

Fills the array(all elements) with the specified value.

Next column gives the overloads for this function
static void fill(boolean[] a, boolean val)Fills the boolean array with a specified boolean value
static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)Assigns a boolean value to the specified range in the boolean array.
static void fill(byte[] a, byte val)Fills the byte array with a specified byte value
static void fill(byte[] a, int fromIndex, int toIndex, byte val)Fills the byte array with specified byte value in the given range
static void fill(char[] a, char val)Fills the char array with specified char value
static void fill(char[] a, int fromIndex, int toIndex, char val)Fills the char array range with specified char value
static void fill(double[] a, double val)Fills the double array with specified double value
static void fill(double[] a, int fromIndex, int toIndex, double val)Assigns a double value to the specified range in the double array.
static void fill(float[] a, float val)Assigns float value to the specified range in the float array.
static void fill(float[] a, int fromIndex, int toIndex, float val)Assigns float value to the specified range in the float array.
static void fill(int[] a, int val)Assigns int value to the int array.
static void fill(int[] a, int fromIndex, int toIndex, int val)Assigns int value to the specified range in the int array.
static void fill(long[] a, int fromIndex, int toIndex, long val)Assigns a long value to the specified range in the long array.
static void fill(long[] a, long val)Assigns a long value to the long array.
static void fill(Object[] a, int fromIndex, int toIndex, Object val)Assigns Object reference to specified range in the Object array.
static void fill(Object[] a, Object val)Assigns Object reference to the specified objectarray
static void fill(short[] a, int fromIndex, int toIndex, short val)Assigns a short value to the specified range in the short array.
static void fill(short[] a, short val)Assigns a short value to the specified short array.
Method NamePrototypeDescription
Sort
Sorts the array passed as a parameter to the method.
Overloads are given in the next column.
static void sort(byte[] a)Sorts the byte array numerically
static void sort(byte[] a, int fromIndex, int toIndex)Sorts the range of elements from the array
static void sort(char[] a)Sorts the character array into ascending numerical order.
static void sort(char[] a, int fromIndex, int toIndex)Sorts the range of elements in the array into ascending order.
static void sort(double[] a)Sorts the double array into ascending numerical order.
static void sort(double[] a, int fromIndex, int toIndex)Sorts the range of elements from the array into ascending order.
static void sort(float[] a)Sorts the float array into ascending numerical order.
static void sort(float[] a, int fromIndex, int toIndex)Sorts the range of elements from the array into ascending order.
static void sort(int[] a)Sorts the int array into ascending numerical order.
static void sort(int[] a, int fromIndex, int toIndex)Sorts therange of elements from the array into ascending order.
static void sort(long[] a)Sorts the long array into ascending numerical order.
static void sort(long[] a, int fromIndex, int toIndex)Sorts the range of elements from the array into ascending order
static void sort(Object[] a)Sorts the array of objects into ascending order. Sorting is done according to the natural ordering of its elements
static void sort(Object[] a, int fromIndex, int toIndex)Sorts the specified range froman array of objects into ascending order. Sorting is done according to the natural ordering of its elements.
static void sort(short[] a)Sorts the array of type short into ascending numerical order.
static void sort(short[] a, int fromIndex, int toIndex)Sorts the range of elements from the array into ascending order.
static < T> void sort(T[] a, Comparator< ? super T> c)Sorts the specified array of objects. The order of sorting is induced as per the specified comparator.
static < T> void sort(T[] a, int fromIndex, int toIndex, Comparator< ? super T> c)Sorts the range of elementsfrom an array of objects in the order specified by the comparator.
Method NamePrototypeDescription
toString

This method returns the string representation of a given array.
Different overloads of this method are given in the next column
static String toString(boolean[] a)Returns a string representation of a boolean array
static String toString(byte[] a)Returns a string representation of a byte array
static String toString(char[] a)Returns a string representation of a character array
static String toString(double[] a)Returns a string representation of a double array
static String toString(float[] a)Returns a string representation of a float array
static String toString(int[] a)Returns a string representation of an int array
static String toString(long[] a)Returns a string representation of a long array
static String toString(Object[] a)Returns a string representation of an object array
static String toString(short[] a)Returns a string representation of a short array
Method NamePrototypeDescription
hashCode
This method returns the hashCode of the contents of the specified array
The overloaded methods are given in the next column.
static int hashCode(boolean[] a)Returns hash code of the contents of the boolean array
static int hashCode(byte[] a)Returns hash code of the contents of the byte array
static int hashCode(char[] a)Returns hash code of the contents of the character array
static int hashCode(double[] a)Returns hash code of the contents of a double array
static int hashCode(float[] a)Returns hash code of the contents of a float array
static int hashCode(int[] a)Returns hash code of the contents of an int array.
static int hashCode(long[] a)Returns hash code of the contents of a long array
static int hashCode(Object[] a)Returns hash code of the contents of object array
static int hashCode(short[] a)Returns hash code of the contents of the short array

The above tables shows all the methods the Arrays class provides. Most of these are overloaded for various primitive types.

Let’s discuss some of these methods in detail.

#1) asList

Prototype: static <T> List<T> asList (Object[] a)
Parameters: a – array of objects from which the list will be backed.
Return Value: List<T> => fixed-size list of specified array

Description: Returns a fixed-size serializable list backed by an array provided as an argument.

Example:

import java.util.Arrays;
import java.util.List;

public class Main {
	public static void main(String[] args) {
		String[] months = {"January", "February", "March", "April", "May"};
		// converted string array to a List using asList
		System.out.println("The string array converted to list:");	
		List&lt;String&gt; month_list = Arrays.asList(months);
		System.out.println(month_list);
	}
}

Output:

asList

The above program demonstrates the usage of the ‘asList’ method of Arrays class. Here, we have declared a string array and passed it to asList method to obtain a list.

#2) binarySearch

Prototype: static int binarySearch (int[] a, int key)

Parameters:

a => array in which the key is to be searched
Key=> element value to be searched

Return Value: int=>position (index) at which key is found, else returns (-(the “insertion point”) – 1).

Description: Searches for the specified key in the given array using a binary search algorithm. The array needs to be sorted for the binary search to work. If the array is not sorted then the results are undefined. Also, if there are multiple locations in the array for the same key value, the position returned is not guaranteed.

Example:

import java.util.Arrays;
import java.util.List;

public class Main { 
    public static void main(String[] args) 
    { 
        // define the Array 
        int numArr[] = { 23,43,26,65,35,16,74,27,98 }; 
        //sort the array first
        Arrays.sort(numArr); 
        System.out.println("Input array:" + Arrays.toString(numArr));

         int key = 35; 
        //call binarySearch function to search a given key
         System.out.println("Key " + key + " found at index = " + Arrays 
                                 .binarySearch(numArr, key)); 
    } 
}

Output:

binarySearch

In the above program first, we sort the input array since for binarySearch the array should be sorted. Then the array and the key to be searched are passed to the ‘binarySearch’ method. The index at which the key is found is displayed in the output.

Prototype: static int binarySearch (int[] a, int fromIndex, int toIndex, int key)

Parameters:

a=> array to be searched
fromIndex=> starting index of the range over which the key is to be searched
toIndex=> the index of the last element in the range
key=> key to be searched for

Return Value: index of the key element is found in the specified range. Otherwise it returns (-(the “insertion point”) – 1).

Description: This overload of binarySearch searches for a key value in the specified range of the array and returns the index position of the key element if found. The array and therefore the range need to be sorted for binarySearch to work. If it’s not sorted, then the results are undefined.

Example:

import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args)
    {
      int numArr[] = { 23,43,26,65,35,16,74,27,98 }; // define the Array
      Arrays.sort(numArr); //sort the array first
      System.out.println("Input array:" + Arrays.toString(numArr));
      int key = 35;
      //call binarySearch function to search a given key
      System.out.println("Key " + key + " found at index = " + Arrays
                 .binarySearch(numArr,3,7, key));
     }
}

Output:

Returns the index position of the key element

The above program is the same as the previous one with a difference that in the call to binarySearch method, we have specified a range of the array in which the search is to be conducted.

#3) copyOf

Prototype: static int[] copyOf(int[] original, int newLength)

Parameters:

original=> array to be copied
newLength=> length of the copied array

Return Value: A new array copied from the original and padded or truncated with zeros depending on a specified length.

Description: Copies the array original into a new array and pads or truncates it with zeros depending on the length specified.

Example:

import java.util.Arrays; 

public class Main { 
    public static void main(String[] args) 
    { 

        // define the Array 
        String strArr[] = {"Java", "Python", "Perl", "C", "Ruby"}; 

        // print the original array 
       System.out.println("Original String Array: "
                           + Arrays.toString(strArr)); 
        //copy the array into new array using copyOf and print it
        System.out.println("Copied Array: "
                           + Arrays.toString(
                      Arrays.copyOf(strArr, 5))); 
    } 
}

Output:

copyOf

The above program demonstrates the use of the ‘copyOf’ method of Arrays class that copies the given array into a new one. The above program copies the original string array into a new array.

#4) copyOfRange

Prototype: static int[] copyOfRange(int[] original, int from, int to)

Parameters:

original => array from which values in the range are to be copied
From=> first index of the range
To=> last index of the range

Return Value: New array with values from the specified range with zeros truncated or padded to obtain the desired length.

Description: Copies the range specified from a given array into a new array. The started index of the array should be inclusive between 0 to original.length. The end index can be exclusive.

Example:

import java.util.Arrays; 

public class Main { 
public static void main(String[] args) 
    { 
         // define the Array 
        String strArr[] = {"Java", "Python", "Perl", "C", "Ruby"}; 

        // print the original array 
       System.out.println("Original String Array: "  + Arrays.toString(strArr)); 
        //copy the array into new array using copyOfRange and print it
       System.out.println("Copied Range of Array: "
                  + Arrays.toString(
                         Arrays.copyOfRange(strArr,1,3))); 
    } 
}

Output:

copyOfRange

We have modified the previous program to use the ‘copyOfRange’ method that copies a specific range from the array and forms a new array. In the above program, we have specified the range like 1, 3. Hence the output shows a new array of 2 elements.

#5) Equals

Prototype: static boolean equals (int [] a, int [] a2)

Parameters:

a=> first array to be tested for equality
A2=> second array to be tested for equality

Return Value: Returns true if both arrays are equal.

Description: This method checks if both the arrays are equal and return the results. The two arrays are said to be equal if both the arrays have an equal number of elements and the corresponding elements in both arrays are equal.

Example:

import java.util.Arrays; 

public class Main {
	public static void main(String[] args) {
		// define two arrays, array_One and array_Two
		int[] array_One = { 1, 3, 5, 7 };
		int[] array_Two = { 1, 3, 5, 7 };
		//print the arrays
		System.out.println("array_One = " + Arrays.toString(array_One));
		System.out.println("array_Two = " + Arrays.toString(array_Two));
		//use equals method to check for equality of arrays		
		booleanarray_equal = Arrays.equals(array_One, array_Two);
		//print the results
		if (array_equal) {
			System.out.println("equals method returns " + array_equal + 
			", hence arrays array_One and array_Two are equal\n");	
		}else {
			System.out.println("equals method returns " + array_equal + 
			", hence arrays array_One and array_Two are not equal\n");
		}
		// define two more arrays, firstArray&amp;secondArray
		int[] firstArray = { 2, 4, 6, 8 };
		int[] secondArray = { 1, 3, 5, 7};
		 //display these arrays
		System.out.println("firstArray = " + Arrays.toString(firstArray));
		System.out.println("secondArray = " + Arrays.toString(secondArray));
		//use equals method to check equality of arrays		
		boolean test_array = Arrays.equals(firstArray, secondArray);
		
		//print the results
		if (test_array) {
			System.out.println("equals method returns " + test_array + 
			", hence arrays firstArray and secondArray are equal\n");	

		}else {
			System.out.println("equals method returns " + test_array + 
			", hence arrays firstArray and secondArray are not equal\n");
		}
	}
}

Output:

Equals

The above program demonstrates the ‘equals’ method. Here, we have used two sets of arrays and called ‘equals’ twice. In the first call to equals, both arrays are the same and hence the method returns true. In the second call to equals, the two arrays are different and the method returns false.

#6) Fill

Prototype: static void fill(int[] a, int val)

Parameters:

a=> array to be filled
val=> value to be filled in all places in array

Return Value: None
Description: Fills the array with the specified value.

Example:

import java.util.Arrays; 

public class Main {
	public static void main(String[] args) {
		// define the array
		int[] intArray = { 1, 3, 5, 7 };
		//print original array
		System.out.println("The original array: " + Arrays.toString(intArray));
		//call fill method to fill the array with all zeros		
		Arrays.fill(intArray, 0);
		//print altered array
		System.out.println("Array after call to fill:" + Arrays.toString(intArray));
	}		
}

Output:

Fill

The above program shows the basic version of the fill method. Here, we just fill the entire array by another value. In this case, we have filled the array with all zeros.

Prototype: static void fill(int[] a, int fromIndex, int toIndex, int val)

Parameters:

a=> array whose range is to be filled
fromIndex => start index of the range
toIndex => end index of the range
val=> value with which the elements in the range is to be filled

Return Value: None

Description: Fills the specified range from fromIndex to toIndex in the array ‘a’ with the specified value. If fromIndex = toIndex, then the range to be filled is empty.

Example:

import java.util.Arrays; 

public class Main {
	public static void main(String[] args) {
		// define the array
		int[] intArray = { 1, 3, 5, 7, 9, 11, 13, 15,17};
		//print original array
		System.out.println("The original array: " + Arrays.toString(intArray));
		//call fill method to fill the range (2,6) in the array with zeros		
		Arrays.fill(intArray, 2, 6, 0);
		//print altered array
		System.out.println("Array after call to fill the range(2,6):" + Arrays.toString(intArray));
	}		
}

Output:

Fills the specified range from fromIndex to toIndex

This is another version of the fill method wherein, we specify the particular range in the array which is to be filled with a different value. In the above program, we have specified the range [2, 6] to be filled with zeros. The other elements remain the same as shown in the output.

#7) Sort

Prototype: static void sort(int[] a)
Parameters: a=> array to be sorted
Return Value: None
Description: This method sorts the array in ascending order.

Example:

import java.util.Arrays; 

public class Main {
	public static void main(String[] args) {
		// define the array
		int[] intArray = {10,4,25,63,21,51,73,24,87,18};
		//print original array
		System.out.println("The original array: " + Arrays.toString(intArray));
		//call sort method to sort the given array in ascending order		
		Arrays.sort(intArray);
		//print altered array
		System.out.println("Sorted array:" + Arrays.toString(intArray));
	}		
}

Output:

Sort

The above program sorts an array of integers using the sort method of Arrays class and prints the sorted array.

Prototype: static void sort(int[] a, int fromIndex, int toIndex)

Parameters:

a=> array from which a range is to be sorted
fromIndex => start index for the range
toIndex=> end index for the range

Return Value: none

Description: Sorts the range specified from fromIndex to toIndex in ascending order. If fromIndex=toIndex, then range to be sorted is empty.

Example:

import java.util.Arrays; 

public class Main {
	public static void main(String[] args) {
		// define the array
		int[] intArray = {10,4,25,63,21,51,73,24,87,18};
		//print original array
		System.out.println("The original array: " + Arrays.toString(intArray));
		//call sort method to sort the given range in the array in ascending order		
		Arrays.sort(intArray, 2, 7);
		//print altered array
		System.out.println("Sorted range(2,7) in the array:" + Arrays.toString(intArray));
	}		
}

Output:

Sorts the range specified from fromIndex to toIndex in ascending order

The above program demonstrates the variation of sort method. In this, we can specify a range over which the array is to be sorted. The elements out of this range are not sorted. In the above program, the range [2,7] in the given array is specified to be sorted in the sort method.

Hence in the output, we can see that only the elements in this range are sorted in ascending order.

#8) toString

Prototype: static String toString(int[] a)
Parameters: a=> array whose string representation is required
Return Value: string=> string representation of array
Description: Converts the given array into its string representation.

Example:

import java.util.*;

public class Main
{
	public static void main(String[] args) {
		//declare arrays of type int and double
		int[] intArray = {10,20,30,40,50};
		
		double[] dblArray = {1.0,2.0,3.0,4.0,5.0};
		
		System.out.println("String representation of int Array: ");
		//print string representation of int array using toString
		System.out.println(Arrays.toString(intArray));
		
		System.out.println("\nString representation of double Array: ");
		 //print string representation of double array using toString	
		System.out.println(Arrays.toString(dblArray));
	}
}

Output:

toString

In the above example, we have used the toString method that converts the arrays to a string representation. So to demonstrate this method, we have used two arrays each of type int and double. Then using the toString method, each of this array is converted to its corresponding string representation shown in the output.

#9) hashCode

Prototype: static int hashCode(int[] a)
Parameters: a=> array whose hashcode is to be computed.
Return Value: int=> hashcode computed

Description: The method returns the hashcode of a given array. The hashcode of a Java Object is actually a 32-bit number (signed int). Using hashcode you can manage an object using hash-based structure.

Hashcode is allocated by JVM to an object and is usually unique unless the two objects are equal to each other in which case both the objects will have the same hashcode.

Example:

import java.util.*;

public class Main
{
	public static void main(String[] args) {
		
		//declare arrays of type int
		int[] intArray = {10,20,30,40,50};
		//print the input array
		System.out.println("The input Array: " + Arrays.toString(intArray));
		//get hashcode of the array using 'hashCode' method of array
		inthashCde = Arrays.hashCode(intArray);
		//print the hashCode	
		System.out.println("The hashCode for input array:" + hashCde);
	}
}

Output:

hashCode

The hashCode method computes the hashcode for the given array passed as an argument to it.

Frequently Asked Questions

Q #1) What are java.util arrays?

Answer: The class java.util.Arrays extend from the class java.lang.Object. The Arrays class contains the method to represent arrays as a list. It also contains various methods to manipulate the arrays like sorting, searching, representing arrays as strings, etc.

Q #2) Which sorting is used in arrays sort in Java?

Answer: The sort method of Arrays class in Java uses two sorting techniques. It uses quicksort when primitive types are used whereas when objects are used that implement comparable interface, merge sort is used.

Q #3) What does Arrays.sort () method do in Java?

Answer: The Arrays.sort () method in Java has various overloads using which you can perform sorting on arrays. It has overloads for sorting different primitive data type’s arrays.

In addition, the Arrays.sort () method has various overloads for sorting an array over a specified range. Apart from this, the Arrays.sort () method also allows us to sort depending on the comparator provided.

Q #4) What are collections and arrays class?

Answer: Collections are dynamic in nature and the class Collections provides direct methods that act on collections. Arrays are static in nature and have class Arrays that provide methods to manipulate arrays.

But these are not direct methods i.e. Array objects cannot invoke these methods. Instead, an array object is passed as an argument to these methods.

Conclusion

Arrays class belongs to java.util package and extends from java.lang.Object class. Arrays class contains methods that are used to manipulate arrays. These methods include those used for sorting arrays, searching for a particular element in arrays, filling the array with a specific value, methods to compare arrays, etc.

Each of these methods has various overloads that allow the programmer to invoke these methods on arrays of different data types and also on part or whole arrays.

In this tutorial, we have discussed most of the methods of arrays class. We also saw a brief description and examples of the major methods. These examples can be replicated for various data types and we leave it you.

=> Visit Here To Learn Java From Scratch.

Was this helpful?

Thanks for your feedback!

Leave a Comment