java.util.Arrays Common Used Methods

(183 Views)


In this Article, we have discussed some commonly used methods of java.util.Arrays

java.util.Arrays methods

Arrays.fill():

fill() method is used to fill values in an array. In absense of this method, we have to loop around and set the values at array indexes manually.
  • fill(array, value): It will set value to all the indexes in array
  • fill(array, startingIdx, endingIdx, value): It will set value to [startingIdx,endingIdx) in array.
import java.util.*; public class JavaTest{ public static void main(String[] args){ int arr[] = {0, 0, 0, 0}; //FILLING VALUES WITH FOR LOOP for(int i=0; i<arr.length; i++) arr[i] = 5; //FILLING VALUES WITH fill() method Arrays.fill(arr, 5); //FILLING LAST 2 VALUES TO 8 Arrays.fill(arr, 2, 4, 8); } }

Arrays.binarySearch():

Arrays.binarySearch() is the most efficient method to search an element in a sorted array.
It can search an element in primitive datatype like int, String, float, etc as well as Objects.
This method returns index of search key. If the value is not present in the array, -1 is returned.
It is important to note that Arrays.binarySearch expects the Array to be sorted, You can sort the array using Arrays.sort() method.
binarySearch has a time complexity of O(logn)
import java.util.*; public class JavaTest{ public static void main(String[] args){ String string[] = {"delhi", "mumbai", "kolkata"}; int idx = Arrays.binarySearch(string, "mumbai"); System.out.print(idx); //prints 1 idx = Arrays.binarySearch(string, "chennai"); System.out.print(idx); //prints -1 } }

Arrays.deepEquals():

This method checks whether the two arrays passed are identical or not. If they are identical, method returns true else return false.
Arrys.equals() is another method in Arrays class but it checks only single dimensional arrays. deepEquals() checks multi-dimensional arrays or you can say nested objects.
If we pass single dimensional array like (int[] or string[] or double[]) it raise an error.
deepEquals() is a very helpful method, when you want to compare two multidimensional arrays. If you have an array 10 levels deep, you have to write 10 nested loops to access the values, this work is done by deepEquals.
import java.util.*; public class JavaTest{ public static void main(String[] args){ int arr1[][] = { {1, 2}, {3, 4}, {5, 6} }; int arr2[][] = { {5, 2}, {3, 8}, {5, 6} }; int arr3[][] = { {1, 2}, {3, 4}, {5, 6} }; System.out.print(Arrays.deepEquals(arr1, arr2)); //prints false System.out.print(Arrays.deepEquals(arr1, arr3)); //prints true } }

Arrays.deepToString():

Just like Arrays.toString() method, which returns string object of single dimensional array, Arrays.deepToString() method returns string object of multi dimensional array.
If you pass a single dimensional array to deepToString() method, it raise an error. The input should be multi-dimensional array strictly.
deepToString() is a very helpful method, when you want to print all the values. If you have an array 10 levels deep, you have to write 10 nested loops to print the values, this work is done by deepEquals.
import java.util.*; public class JavaTest{ public static void main(String[] args){ String names[][] = { {"I","Love"}, {"Java", "Programming"} }; String str = Arrays.deepToString(names); System.out.print(str); //prints [[I, Love], [Java, Programming]] } }

Arrays.asList():

asList() method takes an array as an argument and returns a list with all the elements of array. This method helps to connect primitive array to Collections.
But why would we want to convert Array to List ?
Simple answer would be to use beneficial features of list. Insertion and deletion in an array take more time as in Arrays we have to shift elements. But its not the case with Lists, we can directly insert and delete elements without any shifting.
import java.util.*; public class JavaTest{ public static void main(String[] args){ String names[] = {"rajan", "vinay", "sahil"}; List list = Arrays.asList(names); System.out.print(list.toString()); //prints [rajan, vinay, sahil] } }

Solution Worked 0 UpvotesUpvote

        

Solution Didn't Worked 0 DownvotesDownvote



Comments



Search