Array Class in Java

In programming, Arrays are very important because they facilitate the storage and manipulation of similar types of elements. In Java the Arrays class is an utility class that provides various methods for array objects.

Arrays in Java

An array in Java is a data structure that allows the storage of multiple elements of the same data type (int, char, byte, long…etc) under a single variable name. The Arrays class is a part of Java.util package , contains various methods to manipulated arrays (such as sorting and searching)

Arrays Class Method

The Arrays class in java provides various methods to manipulate array. Lets explore some key methods Available in java Arrays class

Soring Elements of Array

Java Arrays class defines following sort methods to sort elements of primitive and object type arrays.

sort(primitive[] array)

public static void sort( @NotNull  int[] a )

this methods sort the elements in natural sorting order. in the above method signature int type array is given but this method is applicable on others premitive data type.

sort(Object[] a)

public static void sort( @NotNull  Object[] a )

this method sorts the specified array of objects into ascending order, according to the natural ordering of its element

sort(Object[] a, Comparator c )

public static <T> void sort(     @NotNull  T[] a,
    @Nullable  java.util.Comparator<? super T> c )

This method sorts the specified array of objects according to the order induced by the specified comparator. All elements in the array must be mutually comparable by the specified comparator otherwise this method will throw a ClassCastException .

Java Array Sorting example

import java.util.Arrays;
import java.util.Comparator;

public class ArrayCLass {

    public static void main(String[] args) {

        String[] objectArray = {"the","code","data","java","program"};
        System.out.println("Object Array before sorting -"+ Arrays.toString(objectArray));
        Arrays.sort(objectArray);
        System.out.println("Object Array after sorting -"+ Arrays.toString(objectArray));

        int[] primitiveArray = {2,45,6,23,57,89,54,34};
        System.out.println("\nPrimitive Array before sorting -"+           Arrays.toString(primitiveArray));
        Arrays.sort(primitiveArray);
        System.out.println("Primitive Array after sorting -"+ Arrays.toString(primitiveArray));


        System.out.println("\nObject Arrays after sorting by using custom Comparator");
        Arrays.sort(objectArray,new customComparator());
        System.out.println(Arrays.toString(objectArray));

    }


}
class customComparator implements Comparator{

    @Override
    public int compare(Object o1, Object o2) {
        String s1 = o1.toString();
        String s2 = o2.toString();
        return s2.compareTo(s1);
    }
}
Searching Elements in Array

Note – we can only sort Object array by using custom comparator. we can sort premitive array only based on default sorting order.

Searching Elements in Array

For performing searching in array, Array class provide a method named binarySearch() method, but array must be sorted for this method.

In Java Array class defines following search method –

binarySearch(premitive[] p, primitive key)

public static int binarySearch( @NotNull  int[] a, int key )

binarySearch(Object [] a, Object key)

public static int binarySearch(     @NotNull  Object[] a,
    Object key )

binarySearch(Object [] a, Object key,Comparator c)

public static <T> int binarySearch(     @NotNull  T[] a,
 T key,  @Nullable  java.util.Comparator<? super T> c )

Arrays Class binarySearch method Java implementation

import java.util.Arrays;

public class ArrayCLass {

    public static void main(String[] args) {
        String[] objectArray = {"the","code","data","java","program"};
        int[] primitiveArray = {2,45,6,23,57,89,54,34};
//       sorting the array binary search only applicable on sorted array
        Arrays.sort(objectArray);
        Arrays.sort(primitiveArray);

        System.out.println(Arrays.binarySearch(objectArray,"code"));
        System.out.println(Arrays.binarySearch(primitiveArray,5));

    }
}

Arrays Class binarySearch method Java implementation

Similar Java Tutorials

Leave a Comment