A brief Introduction About Arrays Class And its methods

Posted By : Nanda Ram | 23-Jun-2018
It is utility class present in util package, to define several utility methods for arrays for both primitive arrays & object type Arrays.
Sorting the elements of arrays
                Arrays class defines the following methods for this.
1. public static void sort(primitive[] p)
To sort elements of the primitive array according to the natural sorting order
2. public static void sort(Object[] a)
To sort the object array element according to the simple sorting order (natural sorting order )
In this case, compulsory array elements should be the same type(homogeneous type ) & comparable otherwise we will get the class cast exception
3. public static void sort(Object[] a, Comparator c)
  To sort elements of object[] according to customized sorting order.
Note:
    Primitive arrays can be sorted only by natural sorting order whereas object arrays can be sorted either by natural sorting order or by customized sorting order.
 import java.util.*;
import java.lang.*;
import java.io.*;

// A class to represent a student.
class Student {
    int rollno;
    String name, address;

    // Constructor
    public Student(int rollno, String name,
        String address) {
        this.rollno = rollno;
        this.name = name;
        this.address = address;
    }

    // Used to print student details in main()
    public String toString() {
        return this.rollno + " " + this.name +
            " " + this.address;
    }
}

class Sortbyroll implements Comparator < Student > {
    // Used for sorting in ascending order of
    // roll number
    public int compare(Student a, Student b) {
        return a.rollno - b.rollno;
    }
}

// Driver class
class Main {
    public static void main(String[] args) {
        Student[] arr = {
            new Student(111, "bbbb", "london"),
            new Student(131, "aaaa", "nyc"),
            new Student(121, "cccc", "jaipur")
        };

        System.out.println("Unsorted");
        for (int i = 0; i < arr.length; i++)
            System.out.println(arr[i]);

        Arrays.sort(arr, new Sortbyroll());

        System.out.println("\nSorted by rollno");
        for (int i = 0; i < arr.length; i++)
            System.out.println(arr[i]);
    }
}
 Searching the elements of an array
 Arrays class defines the following search methods for this
 1. public static int binarySearch(primitive[] p, primitive key)
 2. public static int binarySearch(Object[] o, Object key)
 3. public static int binarySearch(Object o, Object key, comparator c)
binarySearch() method will search the specified key value into the arrays but the array should be sorted before performing the searching operation on array otherwise we will get the unpredictable result. if there are two key presents in the array then we can not predict which one will be searched.
Note:
  Those rules applicable for binarySearch() method of arrays class exactly same rules are applicable for the collection class binarySearch() method.
 // Java program to demonstrate working of Arrays.
// binarySearch() in a sorted array.
import java.util.Arrays;

public class BinarySearchDemo {
    public static void main(String[] args) {
        byte byteArr[] = { 10, 20, 15, 22,35};
        char charArr[] = {'g','p','q','c','i'};
        int intArr[] = {10,20,15,22,35};
        double doubleArr[] = {10.2,15.1,2.2,3.5
        };
        float floatArr[] = {10.2 f,15.1 f,2.2 f,3.5 f};
        short shortArr[] = {10,20,15,22,35};

        Arrays.sort(byteArr);
        Arrays.sort(charArr);
        Arrays.sort(intArr);
        Arrays.sort(doubleArr);
        Arrays.sort(floatArr);
        Arrays.sort(shortArr);

        byte byteKey = 35;
        char charKey = 'g';
        int intKey = 22;
        double doubleKey = 1.5;
        float floatKey = 35;
        short shortKey = 5;

        System.out.println(byteKey + " found at index = " +
           Arrays.binarySearch(byteArr, byteKey));
        System.out.println(charKey + " found at index = " +
            Arrays.binarySearch(charArr, charKey));
        System.out.println(intKey + " found at index = " +
            Arrays.binarySearch(intArr, intKey));
        System.out.println(doubleKey + " found at index = " +
            Arrays.binarySearch(doubleArr, doubleKey));
        System.out.println(floatKey + " found at index = " +
            Arrays.binarySearch(floatArr, floatKey));
        System.out.println(shortKey + " found at index = " +
            Arrays.binarySearch(shortArr, shortKey));
    }
}
 
Converting arrays to List
1. public static List asList(Object[] a)
By using this method we are not creating an independent list object just we are creating list view for the existing array object
By using List reference if we perform any operation the changes will be reflected in the array reference. similarly, by using array reference if we perform any changes, those changes will reflect the list.
With the help of list reference we can not do any operation which has different size( i.e, add and remove ) otherwise we will get RunTimeException saying unsupported operation exception.
By using List reference we can perform replacement operation but the replacement should be with the same type of element only otherwise we get RunTimeException saying ArrayStoreException 

About Author

Author Image
Nanda Ram

Nanda Ram is a Java Developer, he keep conscientious about his task to complete it in a effective and efficient manner .

Request for Proposal

Name is required

Comment is required

Sending message..