Menu
Topics Index
...
`


Collections Framework >
Siva Nookala - 22 Mar 2016
java.util.Arrays class contains various static methods for manipulating arrays. The manipulation includes searching, sorting, list view of an array and others.

  • Using the asList() method the array can be converted to a List. This method returns an un-modifiable list, so adding elements to that list is not supported.
Declaration of java.util.Arrays.asList() method.
public static <T> List<T> asList(T... a)

Here is a sample program which uses asList method.
Arrays asList Test
import java.util.Arrays;

class ArraysAsListTest
{
    public static void main(String arg[])
    {
        String sample[] = {"MeritCampus", "Siva", "Mahesh", "Kiran"};
        List<String> sampleList = Arrays.asList(sample); // LINE A
        System.out.println(sampleList); // LINE B    
    }
}
OUTPUT

[MeritCampus, Siva, Mahesh, Kiran]

DESCRIPTION

At LINE A, the string array is converted to the list sampleList, which holds all the elements present in array sample. LINE B prints the elements present in the list, same as the elements in the array.

THINGS TO TRY
  • Add elements to the list sampleList after LINE B and print the elements in the list.
  • Remove elements from the list sampleListand print the elements in the list.
  • The sort() method sorts the elements of the specified array in ascending order. It uses a variant of quicksort for sorting. Other variants of sort method are present which sorts the specified range of the specified array.
Declaration of java.util.Arrays.sort() method
public static void sort(T[] array)

Here is a sample program which uses sort method.
Arrays Sort Test
java.util.Arrays;

class ArraysSortTest
{
    public static void main(String arg[])
    {
        int sample[] = {5, 1, 8, 2, 4, 3, 7, 6, 10, 9};
        Arrays.sort(sample, 3, 8); // LINE A
        System.out.print("Array after sorting within specified range is: ");
        for (int temp : sample)
        {
            System.out.print(temp + " ");
        }
        Arrays.sort(sample); // LINE B
        System.out.println();
        System.out.print("Array after sorting all elements is: ");
        for (int temp : sample)
        {
            System.out.print(temp + " ");
        }    
    }
}
OUTPUT

Array after sorting within specified range is: 5 1 8 2 3 4 6 7 10 9
Array after sorting all elements is: 1 2 3 4 5 6 7 8 9 10

DESCRIPTION

At LINE A, the array is sorted in the range [3, 8) i.e. inclusive lower limit and exclusive upper limit. The elements after sorting in the specified range are printed. At LINE B the whole array is sorted. The elements after sorting are printed.

THINGS TO TRY
  • Try changing the sorting range at LINE A. Consider cases like fromIndexgreater than toIndex, fromIndexequal to toIndexand index values out of bounds.
  • Try sorting double array or float array or String array.
  • The binarySearch() method searches the specified array for the specified key using binary search algorithm. Note that the array must be sorted before invoking binarySearch() method. This method returns an integer, on success it returns the index of the key, otherwise, (-(insertion point) - 1) where the insertion point is the point at which the key would be inserted into the array.
Declaration of java.util.Arrays.binarySearch() method
public static int binarySearch(T[] a, T key)

Here is a sample program which uses binarySearch method.
Arrays BinarySearch Test
java.util.Arrays;

class  ArraysBinarySearchTest
{
    public static void main(String arg[])
    {
        int sample[] = {5, 1, 8, 0, 4, 3, 7, 6, 10, 9};
        Arrays.sort(sample); // LINE A
        int index = Arrays.binarySearch(sample, 5); // LINE B
        if (index >= 0)
        {
            System.out.println("Element 5 is found at position: " + index);
        } else
        {
            System.out.println("Element 5 is not found. It can be inserted at position: " + index);
        }
        index = Arrays.binarySearch(sample, 2); //LINE C
        if (index >= 0)
        {
            System.out.println("Element 2 is found at position: " + index);
        } else
        {
            System.out.println("Element 2 is not found. It can be inserted at position: " + index);
        }    
    }
}
OUTPUT

Element 5 is found at position: 4
Element 2 is not found. It can be inserted at position: -3

DESCRIPTION

Initially the array is not sorted, we need to sort the array before searching. Sorting is done at LINE A. At LINE B, binary search is done on the array with search key as 5. 'index' holds the position of the search key in the array. If found, position is printed else element is not found. At LINE C, we are trying to find an element which is not present in the array it returns a values less than 0 i.e. (-(insertion point) - 1) as mentioned above.

THINGS TO TRY
  • Comment LINE A and search for the key and print the result.
  • Use a different data type like float for search key and perform the binary search i.e. search for 5.0f instead of 5
  • Search for the key 14 instead of 2

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App