Introduction
The Arrays
class in Java, part of the java.util
package, provides utility methods for manipulating arrays, including sorting, searching, and comparing.
Table of Contents
- What is the
Arrays
Class? - Common Methods
- Examples of Using the
Arrays
Class - Conclusion
1. What is the Arrays Class?
The Arrays
class provides static methods to work efficiently with arrays, such as sorting, searching, filling, comparing, and converting arrays to strings.
2. Common Methods
sort(T[] a)
: Sorts the specified array into ascending order.binarySearch(T[] a, T key)
: Searches for the specified key in the array using the binary search algorithm.equals(Object[] a, Object[] a2)
: Returnstrue
if the two specified arrays are equal.fill(T[] a, T val)
: Assigns the specified value to each element of the specified array.copyOf(T[] original, int newLength)
: Copies the specified array, truncating or padding with nulls or zeros if necessary.toString(T[] a)
: Returns a string representation of the array.asList(T... a)
: Returns a fixed-size list backed by the specified array.stream(T[] array)
: Returns a sequentialStream
with the specified array as its source.
3. Examples of Using the Arrays Class
Example 1: Sorting an Array
This example demonstrates how to sort an array using the sort
method.
import java.util.Arrays; public class SortExample { public static void main(String[] args) { int[] numbers = {5, 3, 8, 1, 2}; Arrays.sort(numbers); System.out.println("Sorted array: " + Arrays.toString(numbers)); } }
Output:
Sorted array: [1, 2, 3, 5, 8]
Example 2: Searching in an Array
This example shows how to use the binarySearch
method to find an element in a sorted array.
import java.util.Arrays; public class BinarySearchExample { public static void main(String[] args) { int[] numbers = {1, 2, 3, 4, 5}; int index = Arrays.binarySearch(numbers, 3); System.out.println("Index of 3: " + index); } }
Output:
Index of 3: 2
Example 3: Filling an Array
This example demonstrates how to fill an array with a specific value using the fill
method.
import java.util.Arrays; public class FillExample { public static void main(String[] args) { int[] numbers = new int[5]; Arrays.fill(numbers, 7); System.out.println("Filled array: " + Arrays.toString(numbers)); } }
Output:
Filled array: [7, 7, 7, 7, 7]
Example 4: Copying an Array
This example shows how to copy an array using the copyOf
method.
import java.util.Arrays; public class CopyOfExample { public static void main(String[] args) { int[] original = {1, 2, 3}; int[] copy = Arrays.copyOf(original, 5); System.out.println("Copied array: " + Arrays.toString(copy)); } }
Output:
Copied array: [1, 2, 3, 0, 0]
Example 5: Comparing Arrays
This example demonstrates how to compare two arrays using the equals
method.
import java.util.Arrays; public class EqualsExample { public static void main(String[] args) { int[] array1 = {1, 2, 3}; int[] array2 = {1, 2, 3}; boolean areEqual = Arrays.equals(array1, array2); System.out.println("Arrays are equal: " + areEqual); } }
Output:
Arrays are equal: true
Example 6: Converting Array to List
This example shows how to convert an array to a list using the asList
method.
import java.util.Arrays; import java.util.List; public class AsListExample { public static void main(String[] args) { String[] array = {"A", "B", "C"}; List<String> list = Arrays.asList(array); System.out.println("List: " + list); } }
Output:
List: [A, B, C]
Example 7: Streaming an Array
This example demonstrates how to create a stream from an array using the stream
method.
import java.util.Arrays; public class StreamExample { public static void main(String[] args) { String[] array = {"Java", "Python", "C++"}; Arrays.stream(array).forEach(System.out::println); } }
Output:
Java Python C++
4. Conclusion
The Arrays
class in Java provides a wide range of utility methods for efficient array manipulation. From sorting and searching to filling and copying, these methods simplify working with arrays in Java applications.