Created: 2023-09-02 18:47
Status: #concept
Subject: Programming
Tags: Java Class java.util Java Package
java.util.Arrays
This class contains various
static
methods for manipulating primitive T[]
arrays (such as sorting and searching).
Common Methods
These are all
static
, so all we need is to import java.util.Arrays
before using a Arrays.method()
.
sort(T[] arr): void
sorts an array.sort(T[] arr, int from, int to)
sorts a specificfrom
andto
range (exclusive).
equals(T[] arr1, T[] arr2): boolean
checks if arrays are equal.binarySearch(T[] arr, T value): int
searches for avalue
using the Binary Search Algorithm.binarySearch(T[] arr, int from, int to, int value)
does the same as above, but narrows the search in a specificfrom
index and ato
(exclusive).
toString(T[] arr): String
serializes the array into a"[ ... ]"
Java String.
int[] numbers = {8, 3, 7, 9, 1, 2, 4};
System.out.println(Arrays.toString(numbers));
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
[8, 3, 7, 9, 1, 2, 4]
[1, 2, 3, 4, 7, 8, 9]