SwadhyAI Notes

All Java notes

Java Arrays Methods Reference

›Short versionOpen if you only have a minute.
  • An array has a fixed length. That length is a field, values.length, not a method call.
  • Valid indexes run from 0 through length - 1. The index length itself throws.
  • Arrays.sort reorders the array you pass. The old order is gone unless you copied first.
  • Arrays.binarySearch is defined only for an array already sorted in the same order.
  • Arrays.copyOf returns a new array. For objects it copies references, not deep copies of each object.
  • Arrays.asList on an int[] treats the whole array as one list element.

Arrays store a fixed number of elements of one type. The Arrays utility class adds searching, sorting, filling, copying, comparison, and readable string conversion.

Methods

APIParametersReturnsBehaviorWatch for
Arrays.toString(array)Primitive or object one-dimensional array.Readable String representation.Formats elements inside square brackets.Use deepToString() for nested arrays.
Arrays.sort(array)Array, optional range, optional comparator for object arrays.void.Reorders elements in ascending natural or comparator order.Original order is lost unless you copy first.
Arrays.binarySearch(array, key)Sorted array and key.Index if found; otherwise negative insertion point encoding.Performs fast binary search.Array must already be sorted with the same ordering.
Arrays.copyOf(array, newLength)Source array and desired length.New array.Copies existing elements and pads default values if longer.It is a shallow copy for object references.
Arrays.equals(a, b)Two arrays of same compatible type.boolean.Compares length and corresponding elements.Use deepEquals() for nested object arrays.
Arrays.fill(array, value)Array, optional range, fill value.void.Assigns the same value into every selected slot.Filling object arrays repeats the same object reference.

Which call fits

OptionBest forTradeoff
ArrayFixed-size data, primitives, tight loops, and interoperability.No add/remove methods and size cannot change.
ArrayListResizable lists of objects.Cannot directly store primitives without boxing.
StreamDeclarative transformations and aggregations.Less direct for beginner mutation and indexed updates.

Examples

Sort, search, and print

java
int[] scores = {70, 95, 80};
Arrays.sort(scores);

int index = Arrays.binarySearch(scores, 95);
System.out.println(index); // 2
System.out.println(Arrays.toString(scores)); // [70, 80, 95]

Grow by copying

java
String[] names = {"Ada", "Linus"};
names = Arrays.copyOf(names, 3);
names[2] = "Grace";

int[] nums = {3, 1}; then List<int[]> boxed = Arrays.asList(nums); How many elements does that list have?

One. Arrays.asList takes a variable number of reference values. An int[] is one object, so the list's single element is the array itself, not 3 and 1. An Integer[] of the same numbers becomes a two-element list. That list is fixed-size and backed by the array: set writes through, and add throws UnsupportedOperationException.