🌱BeginnerArrays
The Arrays Class
Use java.util.Arrays to sort, search, fill, copy, and print arrays without writing boilerplate loops.
Overview
The java.util.Arrays class provides static utility methods for common array operations. Instead of writing your own sorting algorithm or loop to print an array, Arrays gives you ready-made, optimised implementations.
Key methods: Arrays.sort() sorts in ascending order, Arrays.toString() converts to a printable string, Arrays.copyOf() creates a resized copy, Arrays.fill() fills all elements with one value, and Arrays.binarySearch() finds an element in a sorted array.
Key Points
- •Import: `import java.util.Arrays;`
- •Arrays.sort(arr) - sorts in ascending order (modifies the original array)
- •Arrays.toString(arr) - returns "[1, 2, 3]" format for printing
- •Arrays.copyOf(arr, newLength) - returns a new array; truncates or pads with zeros
- •Arrays.fill(arr, value) - sets every element to value
- •Arrays.binarySearch(sortedArr, key) - returns the index, or negative if not found (array must be sorted first)
- •Arrays.equals(arr1, arr2) - true if both arrays have identical content
Examples
Sort and print
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] nums = {5, 2, 8, 1, 9, 3};
System.out.println(Arrays.toString(nums)); // [5, 2, 8, 1, 9, 3]
Arrays.sort(nums); // sort in place (ascending)
System.out.println(Arrays.toString(nums)); // [1, 2, 3, 5, 8, 9]
// Strings sort alphabetically
String[] names = {"Charlie", "Alice", "Bob"};
Arrays.sort(names);
System.out.println(Arrays.toString(names)); // [Alice, Bob, Charlie]
}
}Arrays.sort modifies the original array. Arrays.toString is the quick way to print arrays.
copyOf, fill, and equals
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] original = {10, 20, 30, 40, 50};
// copyOf - new array, can resize
int[] shorter = Arrays.copyOf(original, 3);
int[] longer = Arrays.copyOf(original, 7); // extra elements = 0
System.out.println(Arrays.toString(shorter)); // [10, 20, 30]
System.out.println(Arrays.toString(longer)); // [10, 20, 30, 40, 50, 0, 0]
// fill
int[] zeros = new int[5];
Arrays.fill(zeros, 42);
System.out.println(Arrays.toString(zeros)); // [42, 42, 42, 42, 42]
// equals
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
System.out.println(a == b); // false (different objects)
System.out.println(Arrays.equals(a, b)); // true (same content)
}
}Use Arrays.equals() to compare array contents - == only compares references.
Binary search
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] sorted = {2, 5, 8, 12, 16, 23, 38, 56};
int idx = Arrays.binarySearch(sorted, 12);
System.out.println("Found 12 at index: " + idx); // 3
int notFound = Arrays.binarySearch(sorted, 10);
System.out.println("10 result: " + notFound); // negative (not found)
}
}binarySearch requires the array to be sorted first. If unsorted, results are undefined.
Common Mistakes
- Printing an array directly with System.out.println(arr) - prints a memory reference like [I@1b6d3586, not values. Use Arrays.toString()
- Calling Arrays.binarySearch on an unsorted array - the result will be incorrect
- Expecting Arrays.copyOf to update the original - it returns a new array, the original is unchanged
Note
Arrays.sort() uses a dual-pivot Quicksort for primitives and TimSort for objects - both are efficient O(n log n) algorithms. There's no need to implement your own sort for basic use.
Exercises
Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.
Loading editor…
Completed: 0 / 4 exercises
Knowledge Check
Question 1 of 4