Arrays utility class example in java

The java.util.Arrays class contains various utility methods for manipulating arrays such as sorting and searching. This class also contains a static factory that allows arrays to be viewed as lists.

The java.util.Arrays class is basically a set of static methods that are all useful for working with any type of arrays. In addition to that, it has got many utility methods for using with arrays such as a method for viewing arrays as lists and methods for printing the contents of an array, whatever be the dimension of the array.

Example of Arrays class

The below example shows you to sort the different type of arrays and convert the array into the List.

package com.java.connect.collection;
 
import java.util.Arrays;
import java.util.List;
 
public class ArraysExample {
    public static void main(String[] args) {
        int[] intArray = { 1, 2, 3, 4, 6, 9, 12, 34, 12 };
        char[] charArray = { 'a', 'o', 'r', 'b', 'u', 'g', 'e' };
        String[] stringArray = { "test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8" };
        // Sort the intArray
        Arrays.sort(intArray);
        for (int i = 0; i < intArray.length; i++) {
            System.out.println("intArray is after sort : " + intArray[i]);
        }
        // Convert the array as List
        List intList = Arrays.asList(intArray);
        System.out.println("Size of the int list is : " + intList.size());
        System.out.println();
        // Sort the charArray
        Arrays.sort(charArray);
        for (int i = 0; i < charArray.length; i++) {
            System.out.println("charArray is after sort : " + charArray[i]);
        }
        // Convert the array as List
        List charList = Arrays.asList(charArray);
        System.out.println("Size of the char list is : " + charList.size());
        System.out.println();
        // Sort the stringArray
        Arrays.sort(stringArray);
        for (int i = 0; i < stringArray.length; i++) {
            System.out.println("stringArray is after sort : " + stringArray[i]);
        }
        // Convert the array as List
        List stringList = Arrays.asList(stringArray);
        System.out.println("Size of the string list is : " + stringList.size());
        System.out.println();
    }
}

The output of the above code will be as:

intArray is after sort : 1
intArray is after sort : 2
intArray is after sort : 3
intArray is after sort : 4
intArray is after sort : 6
intArray is after sort : 9
intArray is after sort : 12
intArray is after sort : 12
intArray is after sort : 34
Size of the int list is : 1
charArray is after sort : a
charArray is after sort : b
charArray is after sort : e
charArray is after sort : g
charArray is after sort : o
charArray is after sort : r
charArray is after sort : u
Size of the char list is : 1
stringArray is after sort : test1
stringArray is after sort : test2
stringArray is after sort : test3
stringArray is after sort : test4
stringArray is after sort : test5
stringArray is after sort : test6
stringArray is after sort : test7
stringArray is after sort : test8
Size of the string list is : 8