top 7 most commonly used Java Methods for Arrays
rajneesh
3 min read
- java

array in Java is a very powerful tool for storing and manipulating groups of related data. because of the methods they provide to manage array and in this post i am going to provide you top 7 most useful methods of array class. for this list i have discussed it with my many Java developer friends, and according to their opinions i have created this list.
No.1: Arrays.toString()
this method is use to Converts an array to a String, making it easy to print out its contents. that why its use to print array content by use of line of code. Arrays.toString() method returns the string representation of the array’s contents,that why its make it easy to print out.
Example:
int[] numbers = {9,6,8};
System.out.println(Arrays.toString(numbers)); // Output: [9,6,8]
No.2: Arrays.sort()
Sorting an array is a common task, and Java’s Arrays.sort()
method makes it a very simple. and It arranges the elements in increasing order, so you don’t have to write your own sorting algorithm to sort array. if you need to sort array in decreasing order then you can use Collections.reverseOrder() Method.
Example:
// increasing order it default way to sort
int[] numbers = {8,5,6,4,7,1,9,2,3};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers)); // Output: [1,2,3,4,5,6,7,8,9]
// sort in decreasing order
Arrays.sort(numbers, Collections.reverseOrder());
System.out.println(Arrays.toString(numbers)); // Output:[9,8,7,6,5,4,3,2,1]
No.3: Arrays.copyOf()
Arrays.copyOf() method is used to create a new array with the same elements as the original. use can also say that it makes a copy of any existing array that is why its name is copyof.
Example:
int[] original = {9,8,7,6,5,4,3,2,1};
int[] copy = Arrays.copyOf(original, original.length);
System.out.println(Arrays.toString(copy)); // Output: [9,8,7,6,5,4,3,2,1]
No.4: Arrays.equals()
Arrays.equals() is used to check if two arrays are equal or not (meaning that they have the same elements in the same order or not), It return true if they are equal, otherwise false.
Example:
int[] firstArray = {1, 2, 3,5,6,5,6};
int[] secondArray = {1, 2, 3,5,6,5,6};
System.out.println(Arrays.equals(firstArray, secondArray)); // Outputs: true
No.5: Arrays.fill()
Arrays.fill() Want to initialize all elements of an array with the same value. this method is for all the lazy coders.
Example:
int[] array = new int[5];
Arrays.fill(array, 42);
System.out.println(Arrays.toString(array)); // Output: [42, 42, 42, 42, 42]
No.6: Arrays.copyOfRange()
Arrays.copyOfRange() this method is similar to copyOf method but it also provides additional feature like you can copy elements with in any range.
Example:
int[] original = {1, 2, 3, 4, 5};
int[] rangeCopy = Arrays.copyOfRange(original, 1, 4);
System.out.println(Arrays.toString(rangeCopy)); // Output: [2, 3, 4]
No.7: Arrays.binarySearch()
Arrays.binarySearch() if your array is sorting and you want to find any element in the array you can use this method to find the index of that element.
Example:
int[] numbers = {1, 2, 3, 4, 5};
int index = Arrays.binarySearch(numbers, 4);
System.out.println(index); // Output: 3
conclusion
in this list i have covered all the most common method of array like sorting, converting to string, binary search, making copy of array etc. according to me and my fellow java developer friends, I have created this list. if you think i have missed any method you can comment it i will cover that method also. you are thinking why i am create top 7 method why not top 10, top 5 because of thala for a reason. comment your most commonly used method in you java developer journey. my favorite one is toSting method because it help to print array using one line of code 😅.if you like this post you can share this post with you friends.