Java - Arrays Class
The Arrays class in Java is a part of the java.util package. It provides utility methods for operations on arrays such as sorting, searching, comparing, and filling. These methods simplify array manipulations, making them more efficient and readable.
Features of the Arrays Class
Provides static methods to manipulate arrays.
Simplifies operations like sorting, searching, and comparison.
Works with both primitive and object arrays.
Common Methods in the Arrays Class
1. Sorting Arrays
a) Arrays.sort()
Sorts the specified array into ascending order.
Syntax:
Arrays.sort(array);
Example:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {5, 3, 8, 1, 9};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers)); // Output: [1, 3, 5, 8, 9]
}
}
2. Searching in Arrays
a) Arrays.binarySearch()
Searches for a specific element in a sorted array using binary search. Returns the index of the element if found, or a negative value if not found.
Syntax:
Arrays.binarySearch(array, key);
Example:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 3, 5, 8, 9};
int index = Arrays.binarySearch(numbers, 5);
System.out.println(index); // Output: 2
}
}
Note: The array must be sorted before using binarySearch().
3. Comparing Arrays
a) Arrays.equals()
Checks if two arrays are equal. Returns true if the arrays have the same length and corresponding elements are equal.
Syntax:
Arrays.equals(array1, array2);
Example:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] array1 = {1, 2, 3};
int[] array2 = {1, 2, 3};
System.out.println(Arrays.equals(array1, array2)); // Output: true
}
}
4. Filling Arrays
a) Arrays.fill()
Fills all elements of the array with a specified value.
Syntax:
Arrays.fill(array, value);
Example:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = new int[5];
Arrays.fill(numbers, 7);
System.out.println(Arrays.toString(numbers)); // Output: [7, 7, 7, 7, 7]
}
}
5. Copying Arrays
a) Arrays.copyOf()
Creates a new array by copying the specified array to a specified length.
Syntax:
Arrays.copyOf(originalArray, newLength);
Example:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
int[] copy = Arrays.copyOf(numbers, 5);
System.out.println(Arrays.toString(copy)); // Output: [1, 2, 3, 0, 0]
}
}
6. Converting Arrays to String
a) Arrays.toString()
Converts an array into a human-readable string.
Syntax:
Arrays.toString(array);
Example:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
System.out.println(Arrays.toString(numbers)); // Output: [1, 2, 3]
}
}
7. Converting Multi-Dimensional Arrays to String
a) Arrays.deepToString()
Converts a multi-dimensional array into a human-readable string.
Syntax:
Arrays.deepToString(array);
Example:
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: [[1, 2], [3, 4]]
}
}
8. Comparing Multi-Dimensional Arrays
a) Arrays.deepEquals()
Checks if two multi-dimensional arrays are equal.
Syntax:
Arrays.deepEquals(array1, array2);
Example:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[][] array1 = {{1, 2}, {3, 4}};
int[][] array2 = {{1, 2}, {3, 4}};
System.out.println(Arrays.deepEquals(array1, array2)); // Output: true
}
}
9. Parallel Sorting
a) Arrays.parallelSort()
Sorts an array using parallel sorting (multi-threaded sorting for large datasets).
Syntax:
Arrays.parallelSort(array);
Example:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {5, 3, 8, 1, 9};
Arrays.parallelSort(numbers);
System.out.println(Arrays.toString(numbers)); // Output: [1, 3, 5, 8, 9]
}
}
Advantages of Using the Arrays Class
Simplifies array manipulation.
Provides optimized implementations for sorting and searching.
Reduces boilerplate code.
Offers utility for both primitive and object arrays.
Conclusion
The Arrays class in Java is a powerful utility for working with arrays. By using its predefined methods, developers can perform complex operations like sorting, searching, and copying efficiently and with minimal code. Mastering these methods can greatly enhance your ability to work with arrays in Java.