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:
    
    public static <T> List<T> asList(T… a)
    public static void sort(int[] a)
    public static int binarySearch(int[] a, int k)
    public static boolean equals(int[] a, int[] a2)
    Java-Arrays
    

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.


package com.journaldev.examples;
import java.util.Arrays;
import java.util.List;
/**
 * Java Arrays Example Program
 *
 * @author pankaj
 *
 */
public class ArraysAsListExample {
	public static void main(String[] args) {
		String[] strings = {"one", "two", "three", "four", "five"};
		// strings array is converted into a List
		List<String> list = Arrays.asList(strings);
		System.out.println(list);
	}
}

Output of above program is:


[one, two, three, four, five]

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.


package com.journaldev.examples;
import java.util.Arrays;
/**
 * Java Arrays Example Program
 *
 * @author pankaj
 *
 */
public class ArraysSortExample {
	public static void main(String[] args) {
		// Sorting Character
		char[] chars = {'B', 'D', 'C', 'A', 'E'};
		Arrays.sort(chars);
		System.out.print("Sorted Characters : ");
		for (char character : chars) {
			System.out.print(character+" ");
		}
		// Sorting Integer
		int[] integers = {5, 2, 1, 4, 3};
		Arrays.sort(integers);
		System.out.print("nSorted Integers : ");
		for (int i : integers) {
			System.out.print(i+" ");
		}
		// Sorting Specific Range of Integers
		int[] ints = {5, 2, 1, 4, 3, 9, 6, 8, 7, 10};
		int fromIndex = 2;
		int toIndex = 7;
		Arrays.sort(ints, fromIndex, toIndex);
		System.out.print("nSorted Integers of Specific Range : ");
		for (int i : ints) {
			System.out.print(i+" ");
		}
	}
}

Output of above program is:


Sorted Characters : A B C D E
Sorted Integers : 1 2 3 4 5
Sorted Integers of Specific Range : 5 2 1 3 4 6 9 8 7 10

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.


package com.journaldev.examples;
import java.util.Arrays;
/**
 * Java Arrays binarySearch
 *
 * @author pankaj
 *
 */
public class ArraysBinarySearchExample {
	public static void main(String[] args) {
		// Searching a value from array of integer
		int[] integers = { 5, 2, 1, 4, 3, 9, 6, 8, 7, 10 };
		int index = Arrays.binarySearch(integers, 2);
		if (index >= 0) {
			System.out.println("Element is found at the index :" + index);
		} else {
			System.out.println("Element is not found");
		}
		// Searching a value from array of integer with specific range
		int fromIndex = 2;
		int toIndex = 7;
		int index2 = Arrays.binarySearch(integers, fromIndex, toIndex, 9);
		if (index2 >= 0) {
			System.out.println("Element is found at the index :" + index2);
		} else {
			System.out.println("Element is not found");
		}
	}
}

Output of above program is:


Element is found at the index :1
Element is found at the index :5

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.


package com.journaldev.examples;
import java.util.Arrays;
/**
 * Java Arrays Example Program
 *
 * @author pankaj
 *
 */
public class ArraysEqualExample {
	public static void main(String[] args) {
		// Compare two arrays of type integer which are equal
		int[] a1 = { 1, 2, 3 };
		int[] a2 = { 1, 2, 3 };
		boolean equal = Arrays.equals(a1, a2);
		if (equal) {
			System.out.println("Arrays a1 and a2 are equal with Result : " + equal);
		}else {
			System.out.println("Arrays a1 and a2 are not equal with Result : " + equal);
		}
		// Compare two arrays of type integer which are not equal
		int[] b1 = { 1, 2, 3 };
		int[] b2 = { 4, 5, 6 };
		boolean equal1 = Arrays.equals(b1, b2);
if (equal1) {
			System.out.println("Arrays b1 and b2 are equal with Result : " + equal1);
		}else {
			System.out.println("Arrays b1 and b2 are not equal with Result : " + equal1);
		}
	}
}

Output of above program is given below.


Arrays a1 and a2 are equal with Result : true
Arrays b1 and b2 are not equal with Result : false

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.


package com.journaldev.examples;
import java.util.Arrays;
/**
 * Java Arrays Example Program
 *
 * @author pankaj
 *
 */
public class ArraysNestedArrayExample {
	public static void main(String[] args) {
		// Compare two nested arrays of type integer which are equal
		int[] a1 = { 1, 2, 3 };
		int[] a2 = { 1, 2, 3 };
		Object[] b1 = {a1};
		Object[] b2 = {a2};
		boolean equal = Arrays.equals(b1, b2);
if (equal) {
			System.out.println("Arrays b1 and b2 are equal with Result : " + equal);
		} else {
			System.out.println("Arrays b1 and b2 are not equal with Result : " + equal);
		}
	}
}

Output of above program is:


Arrays b1 and b2 are not equal with Result : false

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.


package com.journaldev.examples;
import java.util.Arrays;
/**
 * Java Arrays Example Program
 *
 * @author pankaj
 *
 */
public class ArraysNestedArrayExample {
	public static void main(String[] args) {
		// Compare two nested arrays of type integer which are equal
		int[] a1 = { 1, 2, 3 };
		int[] a2 = { 1, 2, 3 };
		Object[] b1 = {a1};
		Object[] b2 = {a2};
		boolean equal = Arrays.deepEquals(b1, b2);
if (equal) {
			System.out.println("Arrays b1 and b2 are equal with Result : " + equal);
		} else {
			System.out.println("Arrays b1 and b2 are not equal with Result : " + equal);
		}
	}
}

Output of above arrays equals program is:


Arrays b1 and b2 are equal with Result : true

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

Reference: API Doc

By admin

Leave a Reply

%d bloggers like this: