Java ‘Arrays’ Class: Most Used Methods with Code Examples

FullStackTips
3 min readFeb 19, 2023
Arrays utility class and its methods.

As a lead Java developer, I am often asked about the most commonly used methods of the Arrays class. The java.util.Arrays class in Java is a utility class that provides methods for working with arrays. It contains various methods that allow you to manipulate, search and sort arrays. In this article, I will discuss some of the most frequently used methods of the Arrays class with code examples.

  1. sort() method:

The sort() method is used to sort an array in ascending order. The method takes an array as a parameter and sorts the elements of the array in ascending order.

Example:

int[] numbers = {5, 3, 7, 1, 4};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));

Output:

[1, 3, 4, 5, 7]

2. binarySearch() method:

The binarySearch() method is used to search an element in a sorted array. It takes two parameters — the array to be searched and the value to be searched for. If the value is found, it returns the index of the element, otherwise, it returns a negative value.

Below is an example.

int[] numbers = {1, 3, 4, 5, 7};
int index = Arrays.binarySearch(numbers, 4);
System.out.println(index);

--

--

FullStackTips

I am full stack developer with over 15 years of experience in various programming languages. https://medium.com/@fullstacktips/membership