
The
Arraysclass in Java is part of thejava.utilpackage and provides utility methods for working with arrays, including sorting, searching, comparing, and filling elements.
Overview of Arrays Class
The
Arraysclass is a final class (cannot be extended).It contains static methods for performing operations on arrays.
It works with primitive and object arrays.
Provides enhanced performance for array manipulations.
Commonly Used Methods in Arrays Class
| Method | Description |
sort(array) | Sorts the array in ascending order. |
binarySearch(array, key) | Searches for a key using binary search (sorted array required). |
equals(arr1, arr2) | Compares two arrays for equality. |
fill(array, value) | Fills the entire array with a specific value. |
copyOf(array, newLength) | Copies an array and resizes it. |
toString(array) | Converts the array to a readable string format. |
asList(array) | Converts an array into a List. |
hashCode(array) | Returns a hash code based on array contents. |
Examples of Arrays Class
Sorting an Array (sort())
The sort() method sorts an array in ascending order.
Example - Sorting an Integer Array
import java.util.Arrays;
public class ArraysSortExample {
public static void main(String[] args) {
int[] numbers = {5, 2, 8, 1, 3};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers)); // Output: [1, 2, 3, 5, 8]
}
}
Sorting an Array of Strings
import java.util.Arrays;
public class StringArraySort {
public static void main(String[] args) {
String[] names = {"Zara", "Alice", "Bob"};
Arrays.sort(names);
System.out.println(Arrays.toString(names)); // Output: [Alice, Bob, Zara]
}
}
Sorting a Subarray
javaCopyEditint[] arr = {10, 5, 20, 15, 30};
Arrays.sort(arr, 1, 4);
System.out.println(Arrays.toString(arr)); // Output: [10, 5, 15, 20, 30]
Here, only elements from index 1 to 3 (excluding 4) are sorted.
Searching in an Array (binarySearch())
binarySearch() is used to find an element in a sorted array.
Example - Finding an Element
import java.util.Arrays;
public class BinarySearchExample {
public static void main(String[] args) {
int[] arr = {1, 3, 5, 7, 9};
int index = Arrays.binarySearch(arr, 5);
System.out.println("Element found at index: " + index); // Output: 2
}
}
🔹 Note: If the element is not found, it returns -(insertion point) - 1.
Comparing Arrays (equals())
equals(arr1, arr2) checks if two arrays are identical.
import java.util.Arrays;
public class CompareArrays {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
int[] arr3 = {3, 2, 1};
System.out.println(Arrays.equals(arr1, arr2)); // Output: true
System.out.println(Arrays.equals(arr1, arr3)); // Output: false
}
}
Filling an Array (fill())
fill() assigns the same value to all elements in the array.
import java.util.Arrays;
public class FillArray {
public static void main(String[] args) {
int[] arr = new int[5];
Arrays.fill(arr, 7); // Fill all elements with 7
System.out.println(Arrays.toString(arr)); // Output: [7, 7, 7, 7, 7]
}
}
You can also fill a specific range:
Arrays.fill(arr, 1, 4, 10);
This fills index 1 to 3 with 10.
6. Copying Arrays (copyOf(), copyOfRange())
copyOf(array, newLength): Creates a new array with a specified length.copyOfRange(array, from, to): Copies a specific range.
import java.util.Arrays;
public class CopyArray {
public static void main(String[] args) {
int[] original = {10, 20, 30, 40, 50};
int[] copied = Arrays.copyOf(original, 3);
System.out.println(Arrays.toString(copied)); // Output: [10, 20, 30]
}
}
Example - Copying a Range
int[] copiedRange = Arrays.copyOfRange(original, 1, 4);
System.out.println(Arrays.toString(copiedRange)); // Output: [20, 30, 40]
Converting Arrays to String (toString())
This method is used for displaying array elements.
Example
import java.util.Arrays;
public class ArrayToString {
public static void main(String[] args) {
String[] fruits = {"Apple", "Banana", "Cherry"};
System.out.println(Arrays.toString(fruits)); // Output: [Apple, Banana, Cherry]
}
}
Converting Arrays to List (asList())
asList() converts an array into a List.
import java.util.Arrays;
import java.util.List;
public class ArrayToList {
public static void main(String[] args) {
String[] colors = {"Red", "Green", "Blue"};
List<String> colorList = Arrays.asList(colors);
System.out.println(colorList); // Output: [Red, Green, Blue]
}
}
🔹 Note: The list returned is fixed-size, meaning you cannot add() or remove() elements.
Generating Hash Code (hashCode())
hashCode() returns a hash code for an array, useful in hash-based collections.
import java.util.Arrays;
public class ArrayHashCode {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
System.out.println(Arrays.hashCode(arr)); // Example Output: 30817
}
}
Multidimensional Arrays Support
The deepToString() and deepEquals() methods handle nested arrays.
Converting 2D Array to String
import java.util.Arrays;
public class MultiDimensionalArray {
public static void main(String[] args) {
int[][] matrix = { {1, 2}, {3, 4} };
System.out.println(Arrays.deepToString(matrix));
// Output: [[1, 2], [3, 4]]
}
}
Conclusion
The
Arraysclass simplifies sorting, searching, filling, and comparing arrays.It is a high-performance alternative to writing custom array operations.
Ideal for working with primitive & object arrays.
About the author
I am a Tech Enthusiast having 13+ years of experience in 𝐈𝐓 as a 𝐂𝐨𝐧𝐬𝐮𝐥𝐭𝐚𝐧𝐭, 𝐂𝐨𝐫𝐩𝐨𝐫𝐚𝐭𝐞 𝐓𝐫𝐚𝐢𝐧𝐞𝐫, 𝐌𝐞𝐧𝐭𝐨𝐫, with 12+ years in training and mentoring in 𝐒𝐨𝐟𝐭𝐰𝐚𝐫𝐞 𝐄𝐧𝐠𝐢𝐧𝐞𝐞𝐫𝐢𝐧𝐠, 𝐃𝐚𝐭𝐚 𝐄𝐧𝐠𝐢𝐧𝐞𝐞𝐫𝐢𝐧𝐠, 𝐓𝐞𝐬𝐭 𝐀𝐮𝐭𝐨𝐦𝐚𝐭𝐢𝐨𝐧 𝐚𝐧𝐝 𝐃𝐚𝐭𝐚 𝐒𝐜𝐢𝐞𝐧𝐜𝐞. I have 𝒕𝒓𝒂𝒊𝒏𝒆𝒅 𝒎𝒐𝒓𝒆 𝒕𝒉𝒂𝒏 10,000+ 𝑰𝑻 𝑷𝒓𝒐𝒇𝒆𝒔𝒔𝒊𝒐𝒏𝒂𝒍𝒔 and 𝒄𝒐𝒏𝒅𝒖𝒄𝒕𝒆𝒅 𝒎𝒐𝒓𝒆 𝒕𝒉𝒂𝒏 500+ 𝒕𝒓𝒂𝒊𝒏𝒊𝒏𝒈 𝒔𝒆𝒔𝒔𝒊𝒐𝒏𝒔 in the areas of 𝐒𝐨𝐟𝐭𝐰𝐚𝐫𝐞 𝐃𝐞𝐯𝐞𝐥𝐨𝐩𝐦𝐞𝐧𝐭, 𝐃𝐚𝐭𝐚 𝐄𝐧𝐠𝐢𝐧𝐞𝐞𝐫𝐢𝐧𝐠, 𝐂𝐥𝐨𝐮𝐝, 𝐃𝐚𝐭𝐚 𝐀𝐧𝐚𝐥𝐲𝐬𝐢𝐬, 𝐃𝐚𝐭𝐚 𝐕𝐢𝐬𝐮𝐚𝐥𝐢𝐳𝐚𝐭𝐢𝐨𝐧𝐬, 𝐀𝐫𝐭𝐢𝐟𝐢𝐜𝐢𝐚𝐥 𝐈𝐧𝐭𝐞𝐥𝐥𝐢𝐠𝐞𝐧𝐜𝐞 𝐚𝐧𝐝 𝐌𝐚𝐜𝐡𝐢𝐧𝐞 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠. I am interested in 𝐰𝐫𝐢𝐭𝐢𝐧𝐠 𝐛𝐥𝐨𝐠𝐬, 𝐬𝐡𝐚𝐫𝐢𝐧𝐠 𝐭𝐞𝐜𝐡𝐧𝐢𝐜𝐚𝐥 𝐤𝐧𝐨𝐰𝐥𝐞𝐝𝐠𝐞, 𝐬𝐨𝐥𝐯𝐢𝐧𝐠 𝐭𝐞𝐜𝐡𝐧𝐢𝐜𝐚𝐥 𝐢𝐬𝐬𝐮𝐞𝐬, 𝐫𝐞𝐚𝐝𝐢𝐧𝐠 𝐚𝐧𝐝 𝐥𝐞𝐚𝐫𝐧𝐢𝐧𝐠 new subjects.
Comments
Join the discussionNo comments yet. Be the first to comment.


