Java Arrays class

The java.util.Arrays class is a utility class (i.e. class contains only static methods) in Java that allows arrays to be viewed as lists.

This class contains various methods for manipulating arrays such as sorting, searching and comparing. This class is part of Java Collections Framework.

The methods in this class all throw a NullPointerException, if the specified array reference is null, except where noted.

The signature of the class is as follows:

public class java.util.Arrays extends java.lang.Object

 

Arrays Class Example

The following example demonstrates some important methods of Arrays class.

ArraysClassDemo.java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Collections;

public class ArraysClassDemo {

public static void main(String[] args) {
// Given Integer array
Integer arr[] = {50, 30, 20, 40, 10};

// To convert the Integer array elements as List
List list = Arrays.asList(arr);
System.out.println("Integer array as list: " + list);

// To sort the array in ascending order
Arrays.sort(arr);
System.out.print("Sorted Array: ");
for (int a : arr) {
System.out.print(a + " ");
}
System.out.println();

// To sort the array in descending order
Arrays.sort(arr, Collections.reverseOrder()); // Java 8 style
list = Arrays.asList(arr);
System.out.println("Sorted Array (reverse order): " + list);

// To search for an element in an array using binary search
int key = 30;
System.out.println("Element " + key
+ " found at index : " + Arrays.binarySearch(arr, key));

// Get the second array
Integer arr2[] = {20, 25, 30};

// To compare both arrays
System.out.println("Integer Arrays on comparison: " + Arrays.equals(arr, arr2));

// Copying an array Using copyOf() and copyOfRange() methods
int[] source = new int[5];
for(int i=0; i < source.length; i++) {
source[i] = i;
}
int[] dest = Arrays.copyOf(source, source.length); //1
dest = Arrays.copyOfRange(source, 0, source.length); //2

// Converting arrays to Strings with toString()
System.out.println("String representation of array: " + Arrays.toString(dest));

// Filling arrays with fill() method
int[] intArray = new int[5];
Arrays.fill(intArray, 99);
System.out.println("Filled array: " + Arrays.toString(intArray));

// Create an ArrayList from a String array
String[] strArray = {"a", "b", "c", "d", "e", "f"};
ArrayList arrList = new ArrayList<>(Arrays.asList(strArray));
System.out.println("Created ArrayList: " + arrList);

// Check if an array contains a certain value
boolean result = Arrays.asList(strArray).contains("c");
System.out.println("Does String array contain 'c'? : " + result);

// Convert an ArrayList to an array
String[] stringArr = new String[arrList.size()];
arrList.toArray(stringArr);
System.out.print("Converted String array: ");
for (String s : stringArr)
System.out.print(s + " ");

System.out.println();

// Convert an array to a set
Set set = new HashSet(Arrays.asList(strArray));
System.out.println("Created Set: " + set);

}
}

 

Output:

Integer array as list: [50, 30, 20, 40, 10]
Sorted Array: 10 20 30 40 50
Sorted Array (reverse order): [50, 40, 30, 20, 10]

Element 30 found at index : 2
Integer Arrays on comparison: false
String representation of array: [0, 1, 2, 3, 4]
Filled array: [99, 99, 99, 99, 99]
Created ArrayList: [a, b, c, d, e, f]
Does String array contain 'c'? : true
Converted String array: a b c d e f
Created Set: [a, b, c, d, e, f]