Arrays Class In Java

Introduction

In this article, we are going to learn arrays class in Java. Arrays class is present in the java.util package. Arrays class concept comes under the collection framework in java. Arrays class provides several utility methods. Using the Arrays class, we can perform searches and can sort and accomplish some other tasks in easy ways.

Prerequisites:

  • Basic knowledge of java
  • Basic knowledge of array in java. You can learn How to use Array in Java by clicking here.

Arrays Class Methods

There are many methods that are used with arrays class. Here we are going to learn some of the important methods.

1. asList() Method

This method will return a list. We have to pass an array object as a parameter in this method.

Example

import java.util.*;

class test {
    public static void main(String args[]) {
        //Int List
        Integer[] arr1 = new Integer[] {
            10,
            20,
            30,
            40,
            50
        };
        List < Integer > list1 = Arrays.asList(arr1);
        System.out.println("Integer List:" + list1);
        //Float list
        Float[] arr2 = new Float[] {
            10.0 f, 20.0 f, 30.0 f, 40.0 f, 50.0 f
        };
        List < Float > list2 = Arrays.asList(arr2);
        System.out.println("Float List:" + list2);
        //String list
        String arr3[] = new String[] {
            "C",
            "C++",
            "Java",
            "Python"
        };
        List < String > list3 = Arrays.asList(arr3);
        System.out.println("String List" + list3);
    }
}

Output

 

Important Point 

When we use the asList() method, we have to use Integer instead of int. If we use int, then we will get an error. The same rule applies to others also.

2. sort() Method

This method is used to sort an array in ascending or descending order.

Example

import java.util.*;

class test {
    public static void main(String args[]) {
        Integer arr1[] = new Integer[] {
            50,
            40,
            30,
            20,
            10
        };
        // Sort Integer Array
        Arrays.sort(arr1);
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i] + " ");
        }
        //For the next line
        System.out.println();
        Float arr2[] = new Float[] {
            50.0 f, 40.0 f, 30.0 f, 20.0 f, 10.0 f
        };
        // Sort Float Array
        Arrays.sort(arr2);
        for (int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i] + " ");
        }
        //For the next line
        System.out.println();
        String arr3[] = new String[] {
            "Python",
            "Java",
            "C",
            "C++"
        };
        //Sort String Array
        Arrays.sort(arr3);
        for (int i = 0; i < arr3.length; i++) {
            System.out.print(arr3[i] + " ");
        }
    }
}

Output

 

3. binarySearch() Method

This method is used to search for an element inside a sorted array. We can apply a binary search on an array, but the array must be in sorted order. In this method, we need to pass two parameters. The first parameter is a sorted array object, and the second is a key. A key is the element that we want to search inside an array. This method will return the index value.

Example

import java.util.*;
class test {
    public static void main(String args[]) {
        Integer arr1[] = new Integer[] {
            10,
            20,
            30,
            40,
            50
        };
        int key = 30;
        int index = Arrays.binarySearch(arr1, key);
        System.out.print(key + " found at index: " + index);
    }
}

Output

 

4. equals() Method

This method is used to compare two arrays and check whether both arrays are equal or not.

Example

import java.util.*;

class test {
    public static void main(String args[]) {
        Integer arr1[] = new Integer[] {
            10,
            20,
            30,
            40,
            50
        };
        Integer arr2[] = new Integer[] {
            10,
            20,
            30,
            40,
            60
        };
        if (Arrays.equals(arr1, arr2)) {
            System.out.println("Both Arrays Are Equal");
        } else {
            System.out.println("Both Arrays Are Not Equal");
        }
    }
}

Output

 

5. compare() Method

This method is used to compare two arrays. If both arrays are equal then it will return a 0 value. Otherwise, it will return a 1 or -1 value.

Example

import java.util.*;

class test {
    public static void main(String args[]) {
        Integer arr1[] = new Integer[] {
            10,
            20,
            30,
            40,
            50
        };
        Integer arr2[] = new Integer[] {
            10,
            20,
            30,
            40,
            60
        };
        if (Arrays.compare(arr1, arr2) == 0) {
            System.out.println("Both arrays are equal");
        } else {
            System.out.println("Both arrays are not equal");
        }
    }
}

Output

 

6. fill() Method

This method is used to assign a particular value to a whole array or within a specified range.

Example 1

import java.util.*;

class test {
    public static void main(String args[]) {
        Integer arr1[] = new Integer[] {
            10,
            20,
            30,
            40,
            50
        };
        Arrays.fill(arr1, 60);
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i] + " ");
        }
    }
}

Here, we assign the value 60 to the whole array.

Output

 

Example 2

import java.util.*;

class test {
    public static void main(String args[]) {
        Integer arr1[] = new Integer[] {
            10,
            20,
            30,
            40,
            50
        };
        Arrays.fill(arr1, 1, 3, 60);
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i] + " ");
        }
    }
}

Output

 

7. mismatch() Method

This method is used to check whether or not two arrays contain a similar element. We have to pass two array objects as a parameter in this method. This method will return the index value, where the first mismatch occurred. 

Example

import java.util.*;

class test {
    public static void main(String args[]) {
        Integer arr1[] = new Integer[] {
            10,
            20,
            30,
            40,
            50
        };
        Integer arr2[] = new Integer[] {
            10,
            20,
            30,
            40,
            60
        };
        int index = Arrays.mismatch(arr1, arr2);
        System.out.println("mismatched index is: " + index);
    }
}

Output

 

8. copyOf() Method

This method is used to create a new array from an existing array, or copy an existing array to a new array. We have to pass two parameters in this method. The first parameter is the existing array object, and the second is the size of the new array.

Example 

import java.util.*;

class test {
    public static void main(String args[]) {
        Integer arr[] = new Integer[] {
            10,
            20,
            30,
            40,
            50
        };
        Integer[] new_array = Arrays.copyOf(arr, 5);
        for (int i = 0; i < new_array.length; i++) {
            System.out.print(new_array[i] + " ");
        }
    }
}

Output

 

Conclusion

In this article we have seen how to perform sorting, searches, and other tasks using the arrays class in Java. Thanks for reading. I hope you liked this article. If you have any suggestions or queries about this article, please share your thoughts. You can read my other articles by clicking here.

Happy learning, friends!


Similar Articles