Java Arrays class consists exclusively of static methods that operates on array.

Java Arrays

  1. Java Arrays class was introduced in Java 1.2 as a utility class to perform various common operations on arrays in java.
  2. This class is part of java collections framework.
  3. Java Arrays class contains methods for sorting, searching and comparing arrays.
  4. Arrays class also contains a static method that returns a list backed by the specified array.
  5. Arrays class contains overloaded methods that support primitive data types. These methods are:

Java Arrays Examples

Let’s have a look at the Arrays methods example through some programs.

Java Arrays asList

Java Arrays provides asList method that returns a list backed by the specified array. Below is a simple program for showing array as a List.

Output of above program is:

Java Arrays sort

Java Arrays provides sort method that sorts the element of specified array and also sorts the specified range of the given array into ascending order.
Below is a simple program for sorting arrays in java.

Output of above program is:

Java Arrays binarySearch

Java Arrays binarySearch method uses binary search algorithm to search specified value from the elements of specified array and also searches from the specified range of the given array.

Output of above program is:

Java Arrays equals

Java Arrays provides equals method that is used to compare two arrays of the same type and returns boolean result. It returns true if two given arrays are equal to one another.

Below is a simple program to compare arrays.

Output of above program is given below.

Compare Nested Arrays

What if the array is inside of another array? Will Arrays equal method is capable to compare nested arrays?

Let’s have a look at the below example program.

Output of above program is:

We can see that the output of the program is false despite the two arrays are equal and contains same number of elements.
Actually equal method is not able to compare nested arrays, for that Arrays have another method called deepEquals.

Let’s have a look at the below example program.

Output of above arrays equals program is:

That’s all for java Arrays class, I hope nothing important got missed here.

Reference: API Doc

By admin

Leave a Reply