The Arrays class contains several utility methods that make array manipulation easier, faster, and more efficient. These methods help reduce code complexity and improve readability.
In this article, we will learn about the Arrays class, its commonly used methods, examples, outputs, advantages, limitations, and best practices.
Table of Contents
What is the Arrays Class in Java?
The Arrays class is a utility class available in the java.util package that provides static methods for performing various operations on arrays.Since all methods are static, there is no need to create an object of the Arrays class. Methods can be called directly using the class name.
Import Statement:
import java.util.Arrays;
Arrays.sort() Method
The sort() method sorts the elements of an array in ascending order.
// Java program to implement sort() method
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {50, 10, 40, 20, 30};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
}
}
Output:
Explanation:[10, 20, 30, 40, 50]
The method rearranges the elements from the smallest value to the largest value.
Arrays.binarySearch() Method
The binarySearch() method searches for a specific element in a sorted array and returns its index.
// Java program to implement binarySearch() method
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
int index = Arrays.binarySearch(numbers, 40);
System.out.println("Index: " + index);
}
}
Output:
Explanation:Index: 3
The method searches for the value 40 and returns its position in the array.
Arrays.equals() Method
The equals() method checks whether two arrays contain the same elements in the same order.
// Java program to implement equals() method
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
boolean result = Arrays.equals(arr1, arr2);
System.out.println(result);
}
}
Output:
Explanation:true
Both arrays contain identical elements in the same sequence, so the method returns true.
Arrays.fill() Method
The fill() method assigns the same value to every element in the array.
// Java program to implement fill() method
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = new int[5];
Arrays.fill(numbers, 100);
System.out.println(Arrays.toString(numbers));
}
}
Output:
Explanation:[100, 100, 100, 100, 100]
All elements of the array are replaced with the value 100.
Arrays.copyOf() Method
The copyOf() method creates a new array by copying elements from an existing array.
// Java program to implement copyOf() method
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] original = {10, 20, 30};
int[] copied = Arrays.copyOf(original, 5);
System.out.println(Arrays.toString(copied));
}
}
Output:
Explanation:[10, 20, 30, 0, 0]
The first three elements are copied, and the remaining positions are filled with default values.
Arrays.copyOfRange() Method
The copyOfRange() method copies a specified range of elements from an array.
// Java program to implement copyOfRange() method
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
int[] result = Arrays.copyOfRange(numbers, 1, 4);
System.out.println(Arrays.toString(result));
}
}
Output:
Explanation:[20, 30, 40]
The method copies elements from index 1 to index 3. The ending index is excluded.
Arrays.toString() Method
The toString() method converts an array into a readable string.
// Java program to implement toString() method
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(numbers));
}
}
Output:
Explanation:[1, 2, 3, 4, 5]
The method displays all array elements in a readable format.
Arrays.deepToString() Method
The deepToString() method is used for printing multidimensional arrays.
// Java program to implement deepToString() method
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1, 2},
{3, 4}
};
System.out.println(Arrays.deepToString(matrix));
}
}
Output:
Explanation:[[1, 2], [3, 4]]
The method prints all nested arrays in a structured format.
Arrays.compare() Method
The compare() method compares two arrays lexicographically.
// Java program to implement compare() method
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 4};
System.out.println(Arrays.compare(arr1, arr2));
}
}
Output:
Explanation:-1
The method returns a negative value because the first array is lexicographically smaller than the second array.
Arrays.mismatch() Method
The mismatch() method returns the index of the first differing element between two arrays.
// Java program to implement mismatch() method
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr1 = {10, 20, 30, 40};
int[] arr2 = {10, 20, 50, 40};
System.out.println(Arrays.mismatch(arr1, arr2));
}
}
Output:
Explanation:2
The arrays first differ at index 2, so the method returns 2.
Advantages of Arrays Class
- Reduces Coding Effort: Built-in methods eliminate the need to write complex array manipulation code manually.
- Improves Readability: Programs become cleaner and easier to understand.
- Provides Efficient Algorithms: Methods such as sort() and binarySearch() use optimized implementations.
- Saves Development Time: Developers can perform common operations with a single method call.
- Supports Multiple Operations: The Arrays class provides methods for sorting, searching, comparing, copying, and displaying arrays.
Limitations of Arrays Class
- Works Only with Arrays: The Arrays class cannot be used directly with collections such as ArrayList.
- Fixed Array Size: The Arrays class does not change the size of an existing array.
- Some Methods Require Sorted Data: Methods such as binarySearch() produce meaningful results only when the array is sorted.
- Limited Modification Features: Advanced operations, such as insertion or deletion, still require manual handling.
Best Practices
- Use Arrays.equals() for Array Comparison: This method compares actual values rather than memory references.
- Sort Before Using binarySearch(): Always sort the array first to obtain accurate search results.
- Use deepToString() for Multidimensional Arrays: This method provides a clear representation of nested arrays.
- Prefer Built-in Methods: Using built-in methods improves performance and reduces coding errors.
- Read Method Documentation: Understanding method behavior helps avoid unexpected results.
Conclusion
The Arrays class in Java provides a collection of useful utility methods for performing common array operations. Methods such as sort(), binarySearch(), equals(), fill(), copyOf(), and toString() simplify array handling and make programs more efficient. Learning these methods is essential for writing clean, readable, and professional Java code.Frequently Asked Questions
1. Which package contains the Arrays class in Java?2. Is it necessary to create an object of the Arrays class?The Arrays class belongs to the java.util package.
3. Can Arrays.sort() sort strings?No, all methods are static and can be called directly using the class name.
4. Why should an array be sorted before using binarySearch()?Yes, the method can sort arrays of strings alphabetically.
5. What is the difference between toString() and deepToString()?Binary search works correctly only on sorted arrays.
toString() is used for one-dimensional arrays, whereas deepToString() is used for multidimensional arrays.
0 Comments