Arrays Class in Java – Free Java Tutorials and Online Training

Arrays Class is a utility class to define several utility methods for Arrays or Arrays objects. Here we discuss about the Arrays Class methods.

Sorting elements of Array

Arrays Class defines the following sort methods to sort elements of Primitive and Object type Arrays:

  • public static void sort(primitive[] p): This method is used to sort elements according to natural sorting order of an Array of primitives.
  • public static void sort(Object[] obj): This method is used to sort according to natural sorting order of an Array of Objects class objects.
  • public static void sort(Object[] obj, Comparator c): This method is used to sort Array according to customized sorting order of an Array of Objects class objects.

Let’s take a simple example of the above methods:

  1. For Sorting Primitive Array in Java:
    import java.util.*;
    public class ArraySrtDmo
    {
        public static void main(String[] args)
        {
            int[] ar={5,25,10,20,15};
            System.out.println("Array Before Sorting : ");
            for(int i:ar)
                System.out.println(i);
            Arrays.sort(ar);
            System.out.println("Array After Sorting : ");
            for(int i:ar)
                System.out.println(i);
        }
    }

    The output will be:

    Array Before Sorting : 
    5
    25
    10
    20
    15
    Array After Sorting : 
    5
    10
    15
    20
    25

  2. For Sorting Object Array in Java :
    import java.util.*;
    public class ArraySrtDmo
    {
        public static void main(String[] args)
        {
            String[] ar={"R","A","J","E","S","H"};
            System.out.println("Array Before Sorting : ");
            for(String i:ar)
                System.out.println(i);
            Arrays.sort(ar);
            System.out.println("Array After Sorting : ");
            for(String i:ar)
                System.out.println(i);
        }
    }

    The output will be:

    Array Before Sorting : 
    R
    A
    J
    E
    S
    H
    Array After Sorting : 
    A
    E
    H
    J
    R
    S

     

Note: We can sort primitive arrays only based on default natural sorting order. Whereas we can sort Object arrays either based on default natural sorting order or based on our customized sorting order (see last two methods among the three methods).


Searching elements of Array in Java using Arrays class

Arrays class defines the following binary search methods:

  • public static int binarySearch(primitive[] p, primitive target)
  • public static int binarySearch(Object[] obj, Object target)
  • public static int binarySearch(Object[] obj, Object target, Comparator c)

Note: All rules of Arrays Class Binary Search methods are exactly same as Collections Class Binary Search methods. That means, we have to first sort the array then we have to search the element. If the element is present in the array the search method will return it’s index, otherwise the insertion point.

Let’s take a simple example:

  1. For primitive Array:
    import java.util.*;
    import static java.util.Arrays.*;
    public class ArraySrchDmo
    {
        public static void main(String[] args)
        {
            int[] ar={5,25,10,20,15};
            Arrays.sort(ar);
            // Searching for rlrmrnt which is present in the Array
            System.out.println(Arrays.binarySearch(ar,5));
            // Searching for rlrmrnt which is not present in the Array
            System.out.println(Arrays.binarySearch(ar,6));
        }
    }

    The output will be:

    0      // Element's index (as 5 is present in the Array)
    -2   // Element's Insertion point (as 6 is not there in the Array)

  2. For Object Array:
    import java.util.*;
    import static java.util.Arrays.*;
    public class ArraySrchDmo
    {
        public static void main(String[] args)
        {
            String[] ar={"R","A","J","E","S","H"};
            Arrays.sort(ar);
            // Searching for element present in the Array
            System.out.println(Arrays.binarySearch(ar,"A"));
            // Searching for element not present in the Array
            System.out.println(Arrays.binarySearch(ar,"B"));
        }
    }

    The output will be:

    0           // As "A" is present in the Array so 0 is the index of "A"
    -2          // As "B" is not there in the Array so -2 is the Insertion point of "B"

     


Conversion of Array to List in Java

  • public static List asList(Object obj):
    Strictly speaking, this method won’t create an independent List object. For the existing Array, we are getting List view.
    i) By using Array reference if we perform any change, automatically the change will be reflected in the Array. Similarly, by using List reference if we perform any change, that change will be reflected in the Array also.
    ii) By using List reference we can’t perform any operation which varies the size otherwise, we will get RuntimeException saying UnsupportedOperationException.
    iii) By using List reference we are not allowed to replace with heterogeneous objects otherwise we will get RuntimeException saying ArrayStoreException.

    import java.util.*;
    import static java.util.Arrays.*;
    public class ArraySrchDmo
    {
        public static void main(String[] args)
        {
            System.out.println("Array View");
            String[] ar={"R","A","J","E","S","H"};
            for(String i:ar)
                System.out.println(i);
            List ls=asList(ar);
            System.out.println("List View");
            System.out.println(ls);
        }
    }

    The output will be:

    Array View
    R
    A
    J
    E
    S
    H
    List View
    [R, A, J, E, S, H]

     


we have covered searching elements and sorting elements in Array using Arrays class methods. For any queries and doubt please mention in below comments.

Leave a Comment

Your email address will not be published. Required fields are marked *