Memra

The Arrays utility class

Sort, search, fill, compare, and print arrays with java.util.Arrays.

java.util.Arrays

The java.util.Arrays class provides static utility methods for common array operations. Import it with import java.util.Arrays;.

int[] nums = {5, 3, 8, 1};

Arrays.sort(nums);                      // sorts in place → [1, 3, 5, 8]
int idx = Arrays.binarySearch(nums, 5); // 2 — array MUST be sorted first

int[] copy = new int[5];
Arrays.fill(copy, 7);                   // [7, 7, 7, 7, 7]

int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
Arrays.equals(a, b);                    // true — element-by-element

System.out.println(Arrays.toString(nums)); // "[1, 3, 5, 8]"

Why you can't just print an array directly:

System.out.println(nums);  // [I@5f4da5c3 — useless

Arrays don't override toString(), so printing one gives a type prefix plus a memory hash. Always wrap in Arrays.toString() for a readable one-line view, or Arrays.deepToString() for 2D arrays.

NORMAL ~/memra/learn/java-from-zero/arrays-utility-class utf-8 LF