Java : Arrays with Examples

Arrays (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Arrays methods.


Summary

This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.

Class diagram

final String[] array = {"aaa", "bbb", "ccc"};

final var str = Arrays.toString(array);
System.out.println(str); // [aaa, bbb, ccc]
final int[] array = {2, 1, 5, 4, 3};

Arrays.sort(array);
System.out.println(Arrays.toString(array)); // [1, 2, 3, 4, 5]
final int[] array1 = {1, 2, 3};
final int[] array2 = {1, 2, 3};

System.out.println(array1.equals(array2)); // false
System.out.println(Arrays.equals(array1, array2)); // true

System.out.println(array1.hashCode()); // 37981645
System.out.println(array2.hashCode()); // 605052357

System.out.println(Arrays.hashCode(array1)); // 30817
System.out.println(Arrays.hashCode(array2)); // 30817

Methods

static <T> List<T> asList (T... a)

Returns a fixed-size list backed by the specified array.

final List<String> list = Arrays.asList("aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
final String[] array = {"aaa", "bbb", "ccc"};
final List<String> list = Arrays.asList(array);
System.out.println(list); // [aaa, bbb, ccc]
final List<Integer> list = Arrays.asList(1, 999, 12345);
System.out.println(list); // [1, 999, 12345]

static int binarySearch (byte[] a, byte key)

Searches the specified array of bytes for the specified value using the binary search algorithm.

This method is equivalent except a type to binarySearch(int[] a, int key).

static int binarySearch (byte[] a, int fromIndex, int toIndex, byte key)

Searches a range of the specified array of bytes for the specified value using the binary search algorithm.

This method is equivalent except a type to binarySearch(int[] a, int fromIndex, int toIndex, int key).

static int binarySearch (char[] a, char key)

Searches the specified array of chars for the specified value using the binary search algorithm.

final char[] a = {'a', 'z', 'b', 'y', 'c', 'x'};

Arrays.sort(a);
System.out.println(Arrays.toString(a)); // [a, b, c, x, y, z]

System.out.println(Arrays.binarySearch(a, 'a')); // 0
System.out.println(Arrays.binarySearch(a, 'b')); // 1
System.out.println(Arrays.binarySearch(a, 'c')); // 2

System.out.println(Arrays.binarySearch(a, 'x')); // 3
System.out.println(Arrays.binarySearch(a, 'y')); // 4
System.out.println(Arrays.binarySearch(a, 'z')); // 5

System.out.println(Arrays.binarySearch(a, 'g')); // -4

static int binarySearch (char[] a, int fromIndex, int toIndex, char key)

Searches a range of the specified array of chars for the specified value using the binary search algorithm.

final char[] a = {'a', 'z', 'b', 'y'};

Arrays.sort(a);
System.out.println(Arrays.toString(a)); // [a, b, y, z]

System.out.println(Arrays.binarySearch(a, 0, 4, 'a')); // 0
System.out.println(Arrays.binarySearch(a, 0, 4, 'b')); // 1
System.out.println(Arrays.binarySearch(a, 0, 4, 'y')); // 2
System.out.println(Arrays.binarySearch(a, 0, 4, 'z')); // 3

System.out.println(Arrays.binarySearch(a, 0, 4, 'g')); // -3

System.out.println(Arrays.binarySearch(a, 1, 4, 'a')); // -2
System.out.println(Arrays.binarySearch(a, 1, 4, 'b')); // 1
System.out.println(Arrays.binarySearch(a, 1, 4, 'y')); // 2
System.out.println(Arrays.binarySearch(a, 1, 4, 'z')); // 3

System.out.println(Arrays.binarySearch(a, 0, 3, 'a')); // 0
System.out.println(Arrays.binarySearch(a, 0, 3, 'b')); // 1
System.out.println(Arrays.binarySearch(a, 0, 3, 'y')); // 2
System.out.println(Arrays.binarySearch(a, 0, 3, 'z')); // -4

static int binarySearch (double[] a, double key)

Searches the specified array of doubles for the specified value using the binary search algorithm.

This method is equivalent except a type to binarySearch(int[] a, int key).

static int binarySearch (double[] a, int fromIndex, int toIndex, double key)

Searches a range of the specified array of doubles for the specified value using the binary search algorithm.

This method is equivalent except a type to binarySearch(int[] a, int fromIndex, int toIndex, int key).

static int binarySearch (float[] a, float key)

Searches the specified array of floats for the specified value using the binary search algorithm.

This method is equivalent except a type to binarySearch(int[] a, int key).

static int binarySearch (float[] a, int fromIndex, int toIndex, float key)

Searches a range of the specified array of floats for the specified value using the binary search algorithm.

This method is equivalent except a type to binarySearch(int[] a, int fromIndex, int toIndex, int key).

static int binarySearch (int[] a, int key)

Searches the specified array of ints for the specified value using the binary search algorithm.

final int[] a = {1, 10, 2, 20, 3, 30};

Arrays.sort(a);
System.out.println(Arrays.toString(a)); // [1, 2, 3, 10, 20, 30]

System.out.println(Arrays.binarySearch(a, 1)); // 0
System.out.println(Arrays.binarySearch(a, 2)); // 1
System.out.println(Arrays.binarySearch(a, 3)); // 2

System.out.println(Arrays.binarySearch(a, 10)); // 3
System.out.println(Arrays.binarySearch(a, 20)); // 4
System.out.println(Arrays.binarySearch(a, 30)); // 5

System.out.println(Arrays.binarySearch(a, 999)); // -7

static int binarySearch (int[] a, int fromIndex, int toIndex, int key)

Searches a range of the specified array of ints for the specified value using the binary search algorithm.

final int[] a = {1, 10, 2, 20};

Arrays.sort(a);
System.out.println(Arrays.toString(a)); // [1, 2, 10, 20]

System.out.println(Arrays.binarySearch(a, 0, 4, 1)); // 0
System.out.println(Arrays.binarySearch(a, 0, 4, 2)); // 1
System.out.println(Arrays.binarySearch(a, 0, 4, 10)); // 2
System.out.println(Arrays.binarySearch(a, 0, 4, 20)); // 3

System.out.println(Arrays.binarySearch(a, 0, 4, 999)); // -5

System.out.println(Arrays.binarySearch(a, 1, 4, 1)); // -2
System.out.println(Arrays.binarySearch(a, 1, 4, 2)); // 1
System.out.println(Arrays.binarySearch(a, 1, 4, 10)); // 2
System.out.println(Arrays.binarySearch(a, 1, 4, 20)); // 3

System.out.println(Arrays.binarySearch(a, 0, 3, 1)); // 0
System.out.println(Arrays.binarySearch(a, 0, 3, 2)); // 1
System.out.println(Arrays.binarySearch(a, 0, 3, 10)); // 2
System.out.println(Arrays.binarySearch(a, 0, 3, 20)); // -4

static int binarySearch (long[] a, int fromIndex, int toIndex, long key)

Searches a range of the specified array of longs for the specified value using the binary search algorithm.

This method is equivalent except a type to binarySearch(int[] a, int fromIndex, int toIndex, int key).

static int binarySearch (long[] a, long key)

Searches the specified array of longs for the specified value using the binary search algorithm.

This method is equivalent except a type to binarySearch(int[] a, int key).

static int binarySearch (short[] a, int fromIndex, int toIndex, short key)

Searches a range of the specified array of shorts for the specified value using the binary search algorithm.

This method is equivalent except a type to binarySearch(int[] a, int fromIndex, int toIndex, int key).

static int binarySearch (short[] a, short key)

Searches the specified array of shorts for the specified value using the binary search algorithm.

This method is equivalent except a type to binarySearch(int[] a, int key).

static int binarySearch (Object[] a, int fromIndex, int toIndex, Object key)

Searches a range of the specified array for the specified object using the binary search algorithm.

final String[] a = {"a", "z", "b", "y"};

Arrays.sort(a);
System.out.println(Arrays.toString(a)); // [a, b, y, z]

System.out.println(Arrays.binarySearch(a, 0, 4, "a")); // 0
System.out.println(Arrays.binarySearch(a, 0, 4, "b")); // 1
System.out.println(Arrays.binarySearch(a, 0, 4, "y")); // 2
System.out.println(Arrays.binarySearch(a, 0, 4, "z")); // 3

System.out.println(Arrays.binarySearch(a, 0, 4, "g")); // -3

System.out.println(Arrays.binarySearch(a, 1, 4, "a")); // -2
System.out.println(Arrays.binarySearch(a, 1, 4, "b")); // 1
System.out.println(Arrays.binarySearch(a, 1, 4, "y")); // 2
System.out.println(Arrays.binarySearch(a, 1, 4, "z")); // 3

System.out.println(Arrays.binarySearch(a, 0, 3, "a")); // 0
System.out.println(Arrays.binarySearch(a, 0, 3, "b")); // 1
System.out.println(Arrays.binarySearch(a, 0, 3, "y")); // 2
System.out.println(Arrays.binarySearch(a, 0, 3, "z")); // -4

static int binarySearch (Object[] a, Object key)

Searches the specified array for the specified object using the binary search algorithm.

final String[] a = {"a", "z", "b", "y", "c", "x"};

Arrays.sort(a);
System.out.println(Arrays.toString(a)); // [a, b, c, x, y, z]

System.out.println(Arrays.binarySearch(a, "a")); // 0
System.out.println(Arrays.binarySearch(a, "b")); // 1
System.out.println(Arrays.binarySearch(a, "c")); // 2

System.out.println(Arrays.binarySearch(a, "x")); // 3
System.out.println(Arrays.binarySearch(a, "y")); // 4
System.out.println(Arrays.binarySearch(a, "z")); // 5

System.out.println(Arrays.binarySearch(a, "g")); // -4

static <T> int binarySearch (T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)

Searches a range of the specified array for the specified object using the binary search algorithm.

record Pair(int first, int second) {
    @Override
    public String toString() {
        return "(%d, %d)".formatted(first, second);
    }
}

class PairComparator implements Comparator<Pair> {
    @Override
    public int compare(Pair o1, Pair o2) {
        if (o1.first() == o2.first()) {
            return o1.second() - o2.second();
        } else {
            return o1.first() - o2.first();
        }
    }
}
final Pair[] a = {
        new Pair(2, 1),
        new Pair(2, 2),
        new Pair(1, 1),
        new Pair(1, 2)
};

final var c = new PairComparator();

Arrays.sort(a, c);
System.out.println(Arrays.toString(a)); // [(1, 1), (1, 2), (2, 1), (2, 2)]

System.out.println(Arrays.binarySearch(a, 0, 4, new Pair(1, 1), c)); // 0
System.out.println(Arrays.binarySearch(a, 0, 4, new Pair(1, 2), c)); // 1
System.out.println(Arrays.binarySearch(a, 0, 4, new Pair(2, 1), c)); // 2
System.out.println(Arrays.binarySearch(a, 0, 4, new Pair(2, 2), c)); // 3

System.out.println(Arrays.binarySearch(a, 0, 4, new Pair(3, 3), c)); // -5

System.out.println(Arrays.binarySearch(a, 1, 4, new Pair(1, 1), c)); // -2
System.out.println(Arrays.binarySearch(a, 1, 4, new Pair(1, 2), c)); // 1
System.out.println(Arrays.binarySearch(a, 1, 4, new Pair(2, 1), c)); // 2
System.out.println(Arrays.binarySearch(a, 1, 4, new Pair(2, 2), c)); // 3

System.out.println(Arrays.binarySearch(a, 0, 3, new Pair(1, 1), c)); // 0
System.out.println(Arrays.binarySearch(a, 0, 3, new Pair(1, 2), c)); // 1
System.out.println(Arrays.binarySearch(a, 0, 3, new Pair(2, 1), c)); // 2
System.out.println(Arrays.binarySearch(a, 0, 3, new Pair(2, 2), c)); // -4

static <T> int binarySearch (T[] a, T key, Comparator<? super T> c)

Searches the specified array for the specified object using the binary search algorithm.

record Pair(int first, int second) {
    @Override
    public String toString() {
        return "(%d, %d)".formatted(first, second);
    }
}

class PairComparator implements Comparator<Pair> {
    @Override
    public int compare(Pair o1, Pair o2) {
        if (o1.first() == o2.first()) {
            return o1.second() - o2.second();
        } else {
            return o1.first() - o2.first();
        }
    }
}
final Pair[] a = {
        new Pair(2, 1),
        new Pair(2, 2),
        new Pair(1, 1),
        new Pair(1, 2)
};

final var c = new PairComparator();

Arrays.sort(a, c);
System.out.println(Arrays.toString(a)); // [(1, 1), (1, 2), (2, 1), (2, 2)]

System.out.println(Arrays.binarySearch(a, new Pair(1, 1), c)); // 0
System.out.println(Arrays.binarySearch(a, new Pair(1, 2), c)); // 1
System.out.println(Arrays.binarySearch(a, new Pair(2, 1), c)); // 2
System.out.println(Arrays.binarySearch(a, new Pair(2, 2), c)); // 3

System.out.println(Arrays.binarySearch(a, new Pair(3, 3), c)); // -5

static int compare (boolean[] a, boolean[] b)

Compares two boolean arrays lexicographically.

final boolean[] a = {false, true, false};
final boolean[] b = {true, false, true};

System.out.println(Arrays.compare(a, a)); // 0
System.out.println(Arrays.compare(a, b)); // -1
System.out.println(Arrays.compare(b, a)); // 1
final boolean[] a = {true, true, true};
final boolean[] b = {true, true};

System.out.println(Arrays.compare(a, b)); // 1
System.out.println(Arrays.compare(b, a)); // -1
final boolean[] a = {false, false, false};
final boolean[] b = {true, true};

System.out.println(Arrays.compare(a, b)); // -1
System.out.println(Arrays.compare(b, a)); // 1

static int compare (boolean[] a, int aFromIndex, int aToIndex, boolean[] b, int bFromIndex, int bToIndex)

Compares two boolean arrays lexicographically over the specified ranges.

final boolean[] a = {true, true, false, false, true, true};
final boolean[] b = {false, false, true, true, false, false};

{
    System.out.println(Arrays.compare(a, 0, 6, a, 0, 6)); // 0
    System.out.println(Arrays.compare(a, 0, 6, b, 0, 6)); // 1
    System.out.println(Arrays.compare(b, 0, 6, a, 0, 6)); // -1
}

{
    System.out.println(Arrays.toString(Arrays.copyOfRange(a, 0, 4))); // [true, true, false, false]
    System.out.println(Arrays.toString(Arrays.copyOfRange(b, 2, 6))); // [true, true, false, false]

    System.out.println(Arrays.compare(a, 0, 4, b, 2, 6)); // 0

    System.out.println(Arrays.toString(Arrays.copyOfRange(a, 2, 4))); // [false, false]
    System.out.println(Arrays.toString(Arrays.copyOfRange(b, 0, 2))); // [false, false]
    System.out.println(Arrays.toString(Arrays.copyOfRange(b, 4, 6))); // [false, false]

    System.out.println(Arrays.compare(a, 2, 4, b, 0, 2)); // 0
    System.out.println(Arrays.compare(a, 2, 4, b, 4, 6)); // 0
}

static int compare (byte[] a, byte[] b)

Compares two byte arrays lexicographically.

This method is equivalent except a type to compare(int[] a, int[] b).

static int compare (byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)

Compares two byte arrays lexicographically over the specified ranges.

This method is equivalent except a type to compare(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static int compare (char[] a, char[] b)

Compares two char arrays lexicographically.

final char[] a = {'a', 'b', 'c', 'd', 'e', 'f'};
final char[] b = {'c', 'd', 'e', 'f', 'a', 'b'};

System.out.println(Arrays.compare(a, a)); // 0
System.out.println(Arrays.compare(a, b)); // -2
System.out.println(Arrays.compare(b, a)); // 2
final char[] a = {'a', 'a', 'a'};
final char[] b = {'a', 'a'};

System.out.println(Arrays.compare(a, b)); // 1
System.out.println(Arrays.compare(b, a)); // -1
final char[] a = {'a', 'a', 'a'};
final char[] b = {'b', 'b'};

System.out.println(Arrays.compare(a, b)); // -1
System.out.println(Arrays.compare(b, a)); // 1

static int compare (char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex)

Compares two char arrays lexicographically over the specified ranges.

final char[] a = {'a', 'b', 'c', 'd', 'e', 'f'};
final char[] b = {'c', 'd', 'e', 'f', 'a', 'b'};

System.out.println(Arrays.compare(a, 0, 6, a, 0, 6)); // 0
System.out.println(Arrays.compare(a, 0, 6, b, 0, 6)); // -2
System.out.println(Arrays.compare(b, 0, 6, a, 0, 6)); // 2

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 2, 6))); // [c, d, e, f]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 0, 4))); // [c, d, e, f]
System.out.println(Arrays.compare(a, 2, 6, b, 0, 4)); // 0

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 0, 2))); // [a, b]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 4, 6))); // [a, b]
System.out.println(Arrays.compare(a, 0, 2, b, 4, 6)); // 0

static int compare (double[] a, double[] b)

Compares two double arrays lexicographically.

This method is equivalent except a type to compare(int[] a, int[] b).

static int compare (double[] a, int aFromIndex, int aToIndex, double[] b, int bFromIndex, int bToIndex)

Compares two double arrays lexicographically over the specified ranges.

This method is equivalent except a type to compare(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static int compare (float[] a, float[] b)

Compares two float arrays lexicographically.

This method is equivalent except a type to compare(int[] a, int[] b).

static int compare (float[] a, int aFromIndex, int aToIndex, float[] b, int bFromIndex, int bToIndex)

Compares two float arrays lexicographically over the specified ranges.

This method is equivalent except a type to compare(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static int compare (int[] a, int[] b)

Compares two int arrays lexicographically.

final int[] a = {0, 1, 2, 3, 4, 5};
final int[] b = {2, 3, 4, 5, 0, 1};

System.out.println(Arrays.compare(a, a)); // 0
System.out.println(Arrays.compare(a, b)); // -1
System.out.println(Arrays.compare(b, a)); // 1
final int[] a = {1, 1, 1};
final int[] b = {1, 1};

System.out.println(Arrays.compare(a, b)); // 1
System.out.println(Arrays.compare(b, a)); // -1
final int[] a = {0, 0, 0};
final int[] b = {1, 1};

System.out.println(Arrays.compare(a, b)); // -1
System.out.println(Arrays.compare(b, a)); // 1

static int compare (int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)

Compares two int arrays lexicographically over the specified ranges.

final int[] a = {0, 1, 2, 3, 4, 5};
final int[] b = {2, 3, 4, 5, 0, 1};

System.out.println(Arrays.compare(a, 0, 6, a, 0, 6)); // 0
System.out.println(Arrays.compare(a, 0, 6, b, 0, 6)); // -1
System.out.println(Arrays.compare(b, 0, 6, a, 0, 6)); // 1

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 2, 6))); // [2, 3, 4, 5]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 0, 4))); // [2, 3, 4, 5]
System.out.println(Arrays.compare(a, 2, 6, b, 0, 4)); // 0

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 0, 2))); // [0, 1]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 4, 6))); // [0, 1]
System.out.println(Arrays.compare(a, 0, 2, b, 4, 6)); // 0

static int compare (long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex)

Compares two long arrays lexicographically over the specified ranges.

This method is equivalent except a type to compare(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static int compare (long[] a, long[] b)

Compares two long arrays lexicographically.

This method is equivalent except a type to compare(int[] a, int[] b).

static int compare (short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex)

Compares two short arrays lexicographically over the specified ranges.

This method is equivalent except a type to compare(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static int compare (short[] a, short[] b)

Compares two short arrays lexicographically.

This method is equivalent except a type to compare(int[] a, int[] b).

static <T extends Comparable<? super T>> int compare (T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex)

Compares two Object arrays lexicographically over the specified ranges.

final String[] a = {"aa", "bb", "cc", "dd", "ee", "ff"};
final String[] b = {"cc", "dd", "ee", "ff", "aa", "bb"};

System.out.println(Arrays.compare(a, 0, 6, a, 0, 6)); // 0
System.out.println(Arrays.compare(a, 0, 6, b, 0, 6)); // -2
System.out.println(Arrays.compare(b, 0, 6, a, 0, 6)); // 2

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 2, 6))); // [cc, dd, ee, ff]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 0, 4))); // [cc, dd, ee, ff]
System.out.println(Arrays.compare(a, 2, 6, b, 0, 4)); // 0

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 0, 2))); // [aa, bb]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 4, 6))); // [aa, bb]
System.out.println(Arrays.compare(a, 0, 2, b, 4, 6)); // 0

static <T> int compare (T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp)

Compares two Object arrays lexicographically over the specified ranges.

record Pair(int first, int second) {
    @Override
    public String toString() {
        return "(%d, %d)".formatted(first, second);
    }
}

class PairComparator implements Comparator<Pair> {
    @Override
    public int compare(Pair o1, Pair o2) {
        if (o1.first() == o2.first()) {
            return o1.second() - o2.second();
        } else {
            return o1.first() - o2.first();
        }
    }
}
final Pair[] a = {
        new Pair(0, 0), new Pair(0, 1), new Pair(0, 2),
        new Pair(1, 0), new Pair(1, 1), new Pair(1, 2)};

final Pair[] b = {
        new Pair(0, 2), new Pair(1, 0), new Pair(1, 1),
        new Pair(1, 2), new Pair(0, 0), new Pair(0, 1)};

final var cmp = new PairComparator();

System.out.println(Arrays.compare(a, 0, 6, a, 0, 6, cmp)); // 0
System.out.println(Arrays.compare(a, 0, 6, b, 0, 6, cmp)); // -2
System.out.println(Arrays.compare(b, 0, 6, a, 0, 6, cmp)); // 2

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 2, 6))); // [(0, 2), (1, 0), (1, 1), (1, 2)]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 0, 4))); // [(0, 2), (1, 0), (1, 1), (1, 2)]
System.out.println(Arrays.compare(a, 2, 6, b, 0, 4, cmp)); // 0

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 0, 2))); // [(0, 0), (0, 1)]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 4, 6))); // [(0, 0), (0, 1)]
System.out.println(Arrays.compare(a, 0, 2, b, 4, 6, cmp)); // 0

static <T extends Comparable<? super T>> int compare (T[] a, T[] b)

Compares two Object arrays, within comparable elements, lexicographically.

final String[] a = {"aa", "bb", "cc", "dd", "ee", "ff"};
final String[] b = {"cc", "dd", "ee", "ff", "aa", "bb"};

System.out.println(Arrays.compare(a, a)); // 0
System.out.println(Arrays.compare(a, b)); // -2
System.out.println(Arrays.compare(b, a)); // 2
final String[] a = {"aa", "aa", "aa"};
final String[] b = {"aa", "aa"};

System.out.println(Arrays.compare(a, b)); // 1
System.out.println(Arrays.compare(b, a)); // -1
final String[] a = {"aa", "aa", "aa"};
final String[] b = {"bb", "bb"};

System.out.println(Arrays.compare(a, b)); // -1
System.out.println(Arrays.compare(b, a)); // 1

static <T> int compare (T[] a, T[] b, Comparator<? super T> cmp)

Compares two Object arrays lexicographically using a specified comparator.

record Pair(int first, int second) {
    @Override
    public String toString() {
        return "(%d, %d)".formatted(first, second);
    }
}

class PairComparator implements Comparator<Pair> {
    @Override
    public int compare(Pair o1, Pair o2) {
        if (o1.first() == o2.first()) {
            return o1.second() - o2.second();
        } else {
            return o1.first() - o2.first();
        }
    }
}
final Pair[] a = {
        new Pair(0, 0), new Pair(0, 1), new Pair(0, 2),
        new Pair(1, 0), new Pair(1, 1), new Pair(1, 2)};

final Pair[] b = {
        new Pair(0, 2), new Pair(1, 0), new Pair(1, 1),
        new Pair(1, 2), new Pair(0, 0), new Pair(0, 1)};

final var cmp = new PairComparator();

System.out.println(Arrays.compare(a, a, cmp)); // 0
System.out.println(Arrays.compare(a, b, cmp)); // -2
System.out.println(Arrays.compare(b, a, cmp)); // 2
final Pair[] a = {new Pair(0, 0), new Pair(0, 0), new Pair(0, 0)};
final Pair[] b = {new Pair(0, 0), new Pair(0, 0)};
final var cmp = new PairComparator();

System.out.println(Arrays.compare(a, b, cmp)); // 1
System.out.println(Arrays.compare(b, a, cmp)); // -1
final Pair[] a = {new Pair(0, 0), new Pair(0, 0), new Pair(0, 0)};
final Pair[] b = {new Pair(0, 1), new Pair(0, 1)};
final var cmp = new PairComparator();

System.out.println(Arrays.compare(a, b, cmp)); // -1
System.out.println(Arrays.compare(b, a, cmp)); // 1

static int compareUnsigned (byte[] a, byte[] b)

Compares two byte arrays lexicographically, numerically treating elements as unsigned.

This method is equivalent except a type to compareUnsigned(int[] a, int[] b).

static int compareUnsigned (byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)

Compares two byte arrays lexicographically over the specified ranges, numerically treating elements as unsigned.

This method is equivalent except a type to compareUnsigned(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static int compareUnsigned (int[] a, int[] b)

Compares two int arrays lexicographically, numerically treating elements as unsigned.

Please see also : compare(int[] a, int[] b)

final int[] a = {1, 1, 1};
final int[] b = {-1, -1, -1};

System.out.println(Arrays.compareUnsigned(a, b)); // -1
System.out.println(Arrays.compare(a, b)); // 1

static int compareUnsigned (int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)

Compares two int arrays lexicographically over the specified ranges, numerically treating elements as unsigned.

Please see also : compare(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)

final int[] a = {1, 1, 1};
final int[] b = {-1, -1, -1};

System.out.println(Arrays.compareUnsigned(a, 0, 3, b, 0, 3)); // -1
System.out.println(Arrays.compare(a, 0, 3, b, 0, 3)); // 1

static int compareUnsigned (long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex)

Compares two long arrays lexicographically over the specified ranges, numerically treating elements as unsigned.

This method is equivalent except a type to compareUnsigned(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static int compareUnsigned (long[] a, long[] b)

Compares two long arrays lexicographically, numerically treating elements as unsigned.

This method is equivalent except a type to compareUnsigned(int[] a, int[] b).

static int compareUnsigned (short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex)

Compares two short arrays lexicographically over the specified ranges, numerically treating elements as unsigned.

This method is equivalent except a type to compareUnsigned(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static int compareUnsigned (short[] a, short[] b)

Compares two short arrays lexicographically, numerically treating elements as unsigned.

This method is equivalent except a type to compareUnsigned(int[] a, int[] b).

static boolean[] copyOf (boolean[] original, int newLength)

Copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length.

final boolean[] original = {true, false, true, false};
System.out.println(Arrays.toString(original)); // [true, false, true, false]

final boolean[] copy1 = Arrays.copyOf(original, 4);
System.out.println(Arrays.toString(copy1)); // [true, false, true, false]

final boolean[] copy2 = Arrays.copyOf(original, 3);
System.out.println(Arrays.toString(copy2)); // [true, false, true]

final boolean[] copy3 = Arrays.copyOf(original, 2);
System.out.println(Arrays.toString(copy3)); // [true, false]

final boolean[] copy4 = Arrays.copyOf(original, 6);
System.out.println(Arrays.toString(copy4)); // [true, false, true, false, false, false]

static byte[] copyOf (byte[] original, int newLength)

Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.

This method is equivalent except a type to copyOf(int[] original, int newLength).

static char[] copyOf (char[] original, int newLength)

Copies the specified array, truncating or padding with null characters (if necessary) so the copy has the specified length.

final char[] original = {'a', 'b', 'c', 'd'};
System.out.println(Arrays.toString(original)); // [a, b, c, d]

final char[] copy1 = Arrays.copyOf(original, 4);
System.out.println(Arrays.toString(copy1)); // [a, b, c, d]

final char[] copy2 = Arrays.copyOf(original, 3);
System.out.println(Arrays.toString(copy2)); // [a, b, c]

final char[] copy3 = Arrays.copyOf(original, 2);
System.out.println(Arrays.toString(copy3)); // [a, b]

final char[] copy4 = Arrays.copyOf(original, 6);
System.out.println(Arrays.toString(copy4)); // [a, b, c, d,  ,  ]

static double[] copyOf (double[] original, int newLength)

Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.

This method is equivalent except a type to copyOf(int[] original, int newLength).

static float[] copyOf (float[] original, int newLength)

Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.

This method is equivalent except a type to copyOf(int[] original, int newLength).

static int[] copyOf (int[] original, int newLength)

Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.

final int[] original = {1, 2, 3, 4};
System.out.println(Arrays.toString(original)); // [1, 2, 3, 4]

final int[] copy1 = Arrays.copyOf(original, 4);
System.out.println(Arrays.toString(copy1)); // [1, 2, 3, 4]

final int[] copy2 = Arrays.copyOf(original, 3);
System.out.println(Arrays.toString(copy2)); // [1, 2, 3]

final int[] copy3 = Arrays.copyOf(original, 2);
System.out.println(Arrays.toString(copy3)); // [1, 2]

final int[] copy4 = Arrays.copyOf(original, 6);
System.out.println(Arrays.toString(copy4)); // [1, 2, 3, 4, 0, 0]

static long[] copyOf (long[] original, int newLength)

Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.

This method is equivalent except a type to copyOf(int[] original, int newLength).

static short[] copyOf (short[] original, int newLength)

Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.

This method is equivalent except a type to copyOf(int[] original, int newLength).

static <T> T[] copyOf (T[] original, int newLength)

Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length.

final String[] original = {"aa", "bb", "cc", "dd"};
System.out.println(Arrays.toString(original)); // [aa, bb, cc, dd]

final String[] copy1 = Arrays.copyOf(original, 4);
System.out.println(Arrays.toString(copy1)); // [aa, bb, cc, dd]

final String[] copy2 = Arrays.copyOf(original, 3);
System.out.println(Arrays.toString(copy2)); // [aa, bb, cc]

final String[] copy3 = Arrays.copyOf(original, 2);
System.out.println(Arrays.toString(copy3)); // [aa, bb]

final String[] copy4 = Arrays.copyOf(original, 6);
System.out.println(Arrays.toString(copy4)); // [aa, bb, cc, dd, null, null]

static <T, U> T[] copyOf (U[] original, int newLength, Class<? extends T[]> newType)

Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length.

final CharSequence[] original = {"aa", "bb", "cc", "dd"};
System.out.println(Arrays.toString(original)); // [aa, bb, cc, dd]

final String[] copy1 = Arrays.copyOf(original, 4, String[].class);
System.out.println(Arrays.toString(copy1)); // [aa, bb, cc, dd]

final String[] copy2 = Arrays.copyOf(original, 3, String[].class);
System.out.println(Arrays.toString(copy2)); // [aa, bb, cc]

final String[] copy3 = Arrays.copyOf(original, 2, String[].class);
System.out.println(Arrays.toString(copy3)); // [aa, bb]

final String[] copy4 = Arrays.copyOf(original, 6, String[].class);
System.out.println(Arrays.toString(copy4)); // [aa, bb, cc, dd, null, null]

static boolean[] copyOfRange (boolean[] original, int from, int to)

Copies the specified range of the specified array into a new array.

final boolean[] original = {true, false, true, false};
System.out.println(Arrays.toString(original)); // [true, false, true, false]

final boolean[] copy1 = Arrays.copyOfRange(original, 0, 4);
System.out.println(Arrays.toString(copy1)); // [true, false, true, false]

final boolean[] copy2 = Arrays.copyOfRange(original, 0, 3);
System.out.println(Arrays.toString(copy2)); // [true, false, true]

final boolean[] copy3 = Arrays.copyOfRange(original, 0, 2);
System.out.println(Arrays.toString(copy3)); // [true, false]

final boolean[] copy4 = Arrays.copyOfRange(original, 0, 6);
System.out.println(Arrays.toString(copy4)); // [true, false, true, false, false, false]

final boolean[] copy5 = Arrays.copyOfRange(original, 1, 4);
System.out.println(Arrays.toString(copy5)); // [false, true, false]

final boolean[] copy6 = Arrays.copyOfRange(original, 2, 4);
System.out.println(Arrays.toString(copy6)); // [true, false]

static byte[] copyOfRange (byte[] original, int from, int to)

Copies the specified range of the specified array into a new array.

This method is equivalent except a type to copyOfRange(int[] original, int from, int to).

static char[] copyOfRange (char[] original, int from, int to)

Copies the specified range of the specified array into a new array.

final char[] original = {'a', 'b', 'c', 'd'};
System.out.println(Arrays.toString(original)); // [a, b, c, d]

final char[] copy1 = Arrays.copyOfRange(original, 0, 4);
System.out.println(Arrays.toString(copy1)); // [a, b, c, d]

final char[] copy2 = Arrays.copyOfRange(original, 0, 3);
System.out.println(Arrays.toString(copy2)); // [a, b, c]

final char[] copy3 = Arrays.copyOfRange(original, 0, 2);
System.out.println(Arrays.toString(copy3)); // [a, b]

final char[] copy4 = Arrays.copyOfRange(original, 0, 6);
System.out.println(Arrays.toString(copy4)); // [a, b, c, d,  ,  ]

final char[] copy5 = Arrays.copyOfRange(original, 1, 4);
System.out.println(Arrays.toString(copy5)); // [b, c, d]

final char[] copy6 = Arrays.copyOfRange(original, 2, 4);
System.out.println(Arrays.toString(copy6)); // [c, d]

static double[] copyOfRange (double[] original, int from, int to)

Copies the specified range of the specified array into a new array.

This method is equivalent except a type to copyOfRange(int[] original, int from, int to).

static float[] copyOfRange (float[] original, int from, int to)

Copies the specified range of the specified array into a new array.

This method is equivalent except a type to copyOfRange(int[] original, int from, int to).

static int[] copyOfRange (int[] original, int from, int to)

Copies the specified range of the specified array into a new array.

final int[] original = {1, 2, 3, 4};
System.out.println(Arrays.toString(original)); // [1, 2, 3, 4]

final int[] copy1 = Arrays.copyOfRange(original, 0, 4);
System.out.println(Arrays.toString(copy1)); // [1, 2, 3, 4]

final int[] copy2 = Arrays.copyOfRange(original, 0, 3);
System.out.println(Arrays.toString(copy2)); // [1, 2, 3]

final int[] copy3 = Arrays.copyOfRange(original, 0, 2);
System.out.println(Arrays.toString(copy3)); // [1, 2]

final int[] copy4 = Arrays.copyOfRange(original, 0, 6);
System.out.println(Arrays.toString(copy4)); // [1, 2, 3, 4, 0, 0]

final int[] copy5 = Arrays.copyOfRange(original, 1, 4);
System.out.println(Arrays.toString(copy5)); // [2, 3, 4]

final int[] copy6 = Arrays.copyOfRange(original, 2, 4);
System.out.println(Arrays.toString(copy6)); // [3, 4]

static long[] copyOfRange (long[] original, int from, int to)

Copies the specified range of the specified array into a new array.

This method is equivalent except a type to copyOfRange(int[] original, int from, int to).

static short[] copyOfRange (short[] original, int from, int to)

Copies the specified range of the specified array into a new array.

This method is equivalent except a type to copyOfRange(int[] original, int from, int to).

static <T> T[] copyOfRange (T[] original, int from, int to)

Copies the specified range of the specified array into a new array.

final String[] original = {"aa", "bb", "cc", "dd"};
System.out.println(Arrays.toString(original)); // [aa, bb, cc, dd]

final String[] copy1 = Arrays.copyOfRange(original, 0, 4);
System.out.println(Arrays.toString(copy1)); // [aa, bb, cc, dd]

final String[] copy2 = Arrays.copyOfRange(original, 0, 3);
System.out.println(Arrays.toString(copy2)); // [aa, bb, cc]

final String[] copy3 = Arrays.copyOfRange(original, 0, 2);
System.out.println(Arrays.toString(copy3)); // [aa, bb]

final String[] copy4 = Arrays.copyOfRange(original, 0, 6);
System.out.println(Arrays.toString(copy4)); // [aa, bb, cc, dd, null, null]

final String[] copy5 = Arrays.copyOfRange(original, 1, 4);
System.out.println(Arrays.toString(copy5)); // [bb, cc, dd]

final String[] copy6 = Arrays.copyOfRange(original, 2, 4);
System.out.println(Arrays.toString(copy6)); // [cc, dd]

static <T, U> T[] copyOfRange (U[] original, int from, int to, Class<? extends T[]> newType)

Copies the specified range of the specified array into a new array.

final CharSequence[] original = {"aa", "bb", "cc", "dd"};
System.out.println(Arrays.toString(original)); // [aa, bb, cc, dd]

final String[] copy1 = Arrays.copyOfRange(original, 0, 4, String[].class);
System.out.println(Arrays.toString(copy1)); // [aa, bb, cc, dd]

final String[] copy2 = Arrays.copyOfRange(original, 0, 3, String[].class);
System.out.println(Arrays.toString(copy2)); // [aa, bb, cc]

final String[] copy3 = Arrays.copyOfRange(original, 0, 2, String[].class);
System.out.println(Arrays.toString(copy3)); // [aa, bb]

final String[] copy4 = Arrays.copyOfRange(original, 0, 6, String[].class);
System.out.println(Arrays.toString(copy4)); // [aa, bb, cc, dd, null, null]

final String[] copy5 = Arrays.copyOfRange(original, 1, 4, String[].class);
System.out.println(Arrays.toString(copy5)); // [bb, cc, dd]

final String[] copy6 = Arrays.copyOfRange(original, 2, 4, String[].class);
System.out.println(Arrays.toString(copy6)); // [cc, dd]

static boolean deepEquals (Object[] a1, Object[] a2)

Returns true if the two specified arrays are deeply equal to one another.

final int[][] a = {{1, 2}, {3, 4}, {5, 6}};
System.out.println(Arrays.deepToString(a)); // [[1, 2], [3, 4], [5, 6]]

final int[][] b = {{1, 2}, {3, 4}, {5, 6}};
System.out.println(Arrays.deepToString(b)); // [[1, 2], [3, 4], [5, 6]]

System.out.println(Arrays.deepEquals(a, b)); // true
System.out.println(Arrays.equals(a, b)); // false

final int[][] c = {{10, 20}, {30, 40}, {50, 60}};
System.out.println(Arrays.deepToString(c)); // [[10, 20], [30, 40], [50, 60]]

System.out.println(Arrays.deepEquals(a, c)); // false

static int deepHashCode (Object[] a)

Returns a hash code based on the "deep contents" of the specified array.

final int[][] a = {{1, 2}, {3, 4}, {5, 6}};
System.out.println(Arrays.deepToString(a)); // [[1, 2], [3, 4], [5, 6]]

final int[][] b = {{1, 2}, {3, 4}, {5, 6}};
System.out.println(Arrays.deepToString(b)); // [[1, 2], [3, 4], [5, 6]]

System.out.println(Arrays.deepHashCode(a)); // 1018945
System.out.println(Arrays.deepHashCode(b)); // 1018945

System.out.println(Arrays.hashCode(a)); // 566646003
System.out.println(Arrays.hashCode(b)); // 1569176015

static String deepToString (Object[] a)

Returns a string representation of the "deep contents" of the specified array.

final int[][] a = {{1, 2}, {3, 4}, {5, 6}};

System.out.println(Arrays.deepToString(a)); // [[1, 2], [3, 4], [5, 6]]
System.out.println(Arrays.toString(a)); // [[I@60db1c0e, [I@3e78b6a5, [I@769a1df5]

static boolean equals (boolean[] a, boolean[] a2)

Returns true if the two specified arrays of booleans are equal to one another.

final boolean[] a = {true, true, true};
final boolean[] a2 = {true, true, true};

System.out.println(Arrays.equals(a, a2)); // true
final boolean[] a = {false, true, false};
final boolean[] a2 = {true, false, true};

System.out.println(Arrays.equals(a, a2)); // false
final boolean[] a = {true, true, true};
final boolean[] a2 = {true, true};

System.out.println(Arrays.equals(a, a2)); // false

static boolean equals (boolean[] a, int aFromIndex, int aToIndex, boolean[] b, int bFromIndex, int bToIndex)

Returns true if the two specified arrays of booleans, over the specified ranges, are equal to one another.

final boolean[] a = {true, true, false, false, true, true};
final boolean[] b = {false, false, true, true, false, false};

System.out.println(Arrays.equals(a, 0, 6, b, 0, 6)); // false

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 0, 4))); // [true, true, false, false]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 2, 6))); // [true, true, false, false]
System.out.println(Arrays.equals(a, 0, 4, b, 2, 6)); // true

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 2, 4))); // [false, false]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 0, 2))); // [false, false]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 4, 6))); // [false, false]
System.out.println(Arrays.equals(a, 2, 4, b, 0, 2)); // true
System.out.println(Arrays.equals(a, 2, 4, b, 4, 6)); // true

static boolean equals (byte[] a, byte[] a2)

Returns true if the two specified arrays of bytes are equal to one another.

This method is equivalent except a type to equals(int[] a, int[] a2).

static boolean equals (byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)

Returns true if the two specified arrays of bytes, over the specified ranges, are equal to one another.

This method is equivalent except a type to equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static boolean equals (char[] a, char[] a2)

Returns true if the two specified arrays of chars are equal to one another.

final char[] a = {'a', 'b', 'c', 'd', 'e', 'f'};
final char[] a2 = {'a', 'b', 'c', 'd', 'e', 'f'};

System.out.println(Arrays.equals(a, a2)); // true
final char[] a = {'a', 'b', 'c', 'd', 'e', 'f'};
final char[] a2 = {'c', 'd', 'e', 'f', 'a', 'b'};

System.out.println(Arrays.equals(a, a2)); // false
final char[] a = {'a', 'a', 'a'};
final char[] a2 = {'a', 'a'};

System.out.println(Arrays.equals(a, a2)); // false

static boolean equals (char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex)

Returns true if the two specified arrays of chars, over the specified ranges, are equal to one another.

final char[] a = {'a', 'b', 'c', 'd', 'e', 'f'};
final char[] b = {'c', 'd', 'e', 'f', 'a', 'b'};

System.out.println(Arrays.equals(a, 0, 6, b, 0, 6)); // false

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 2, 6))); // [c, d, e, f]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 0, 4))); // [c, d, e, f]
System.out.println(Arrays.equals(a, 2, 6, b, 0, 4)); // true

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 0, 2))); // [a, b]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 4, 6))); // [a, b]
System.out.println(Arrays.equals(a, 0, 2, b, 4, 6)); // true

static boolean equals (double[] a, double[] a2)

Returns true if the two specified arrays of doubles are equal to one another.

This method is equivalent except a type to equals(int[] a, int[] a2).

static boolean equals (double[] a, int aFromIndex, int aToIndex, double[] b, int bFromIndex, int bToIndex)

Returns true if the two specified arrays of doubles, over the specified ranges, are equal to one another.

This method is equivalent except a type to equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static boolean equals (float[] a, float[] a2)

Returns true if the two specified arrays of floats are equal to one another.

This method is equivalent except a type to equals(int[] a, int[] a2).

static boolean equals (float[] a, int aFromIndex, int aToIndex, float[] b, int bFromIndex, int bToIndex)

Returns true if the two specified arrays of floats, over the specified ranges, are equal to one another.

This method is equivalent except a type to equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static boolean equals (int[] a, int[] a2)

Returns true if the two specified arrays of ints are equal to one another.

final int[] a = {0, 1, 2, 3, 4, 5};
final int[] a2 = {0, 1, 2, 3, 4, 5};

System.out.println(Arrays.equals(a, a2)); // true
final int[] a = {0, 1, 2, 3, 4, 5};
final int[] a2 = {2, 3, 4, 5, 0, 1};

System.out.println(Arrays.equals(a, a2)); // false
final int[] a = {1, 1, 1};
final int[] a2 = {1, 1};

System.out.println(Arrays.equals(a, a2)); // false

static boolean equals (int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)

Returns true if the two specified arrays of ints, over the specified ranges, are equal to one another.

final int[] a = {0, 1, 2, 3, 4, 5};
final int[] b = {2, 3, 4, 5, 0, 1};

System.out.println(Arrays.equals(a, 0, 6, b, 0, 6)); // false

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 2, 6))); // [2, 3, 4, 5]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 0, 4))); // [2, 3, 4, 5]
System.out.println(Arrays.equals(a, 2, 6, b, 0, 4)); // true

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 0, 2))); // [0, 1]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 4, 6))); // [0, 1]
System.out.println(Arrays.equals(a, 0, 2, b, 4, 6)); // true

static boolean equals (long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex)

Returns true if the two specified arrays of longs, over the specified ranges, are equal to one another.

This method is equivalent except a type to equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static boolean equals (long[] a, long[] a2)

Returns true if the two specified arrays of longs are equal to one another.

This method is equivalent except a type to equals(int[] a, int[] a2).

static boolean equals (short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex)

Returns true if the two specified arrays of shorts, over the specified ranges, are equal to one another.

This method is equivalent except a type to equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static boolean equals (short[] a, short[] a2)

Returns true if the two specified arrays of shorts are equal to one another.

This method is equivalent except a type to equals(int[] a, int[] a2).

static boolean equals (Object[] a, int aFromIndex, int aToIndex, Object[] b, int bFromIndex, int bToIndex)

Returns true if the two specified arrays of Objects, over the specified ranges, are equal to one another.

final String[] a = {"aa", "bb", "cc", "dd", "ee", "ff"};
final String[] b = {"cc", "dd", "ee", "ff", "aa", "bb"};

System.out.println(Arrays.equals(a, 0, 6, b, 0, 6)); // false

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 2, 6))); // [cc, dd, ee, ff]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 0, 4))); // [cc, dd, ee, ff]
System.out.println(Arrays.equals(a, 2, 6, b, 0, 4)); // true

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 0, 2))); // [aa, bb]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 4, 6))); // [aa, bb]
System.out.println(Arrays.equals(a, 0, 2, b, 4, 6)); // true

static boolean equals (Object[] a, Object[] a2)

Returns true if the two specified arrays of Objects are equal to one another.

final String[] a = {"aa", "bb", "cc", "dd", "ee", "ff"};
final String[] a2 = {"aa", "bb", "cc", "dd", "ee", "ff"};

System.out.println(Arrays.equals(a, a2)); // true
final String[] a = {"aa", "bb", "cc", "dd", "ee", "ff"};
final String[] a2 = {"cc", "dd", "ee", "ff", "aa", "bb"};

System.out.println(Arrays.equals(a, a2)); // false
final String[] a = {"aa", "aa", "aa"};
final String[] a2 = {"aa", "aa"};

System.out.println(Arrays.equals(a, a2)); // false

static <T> boolean equals (T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp)

Returns true if the two specified arrays of Objects, over the specified ranges, are equal to one another.

record Pair(int first, int second) {
    @Override
    public String toString() {
        return "(%d, %d)".formatted(first, second);
    }
}

class PairComparator implements Comparator<Pair> {
    @Override
    public int compare(Pair o1, Pair o2) {
        if (o1.first() == o2.first()) {
            return o1.second() - o2.second();
        } else {
            return o1.first() - o2.first();
        }
    }
}
final Pair[] a = {
        new Pair(0, 0), new Pair(0, 1), new Pair(0, 2),
        new Pair(1, 0), new Pair(1, 1), new Pair(1, 2)};

final Pair[] b = {
        new Pair(0, 2), new Pair(1, 0), new Pair(1, 1),
        new Pair(1, 2), new Pair(0, 0), new Pair(0, 1)};

final var cmp = new PairComparator();

System.out.println(Arrays.equals(a, 0, 6, b, 0, 6, cmp)); // false

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 2, 6))); // [(0, 2), (1, 0), (1, 1), (1, 2)]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 0, 4))); // [(0, 2), (1, 0), (1, 1), (1, 2)]
System.out.println(Arrays.equals(a, 2, 6, b, 0, 4, cmp)); // true

System.out.println(Arrays.toString(Arrays.copyOfRange(a, 0, 2))); // [(0, 0), (0, 1)]
System.out.println(Arrays.toString(Arrays.copyOfRange(b, 4, 6))); // [(0, 0), (0, 1)]
System.out.println(Arrays.equals(a, 0, 2, b, 4, 6, cmp)); // true

static <T> boolean equals (T[] a, T[] a2, Comparator<? super T> cmp)

Returns true if the two specified arrays of Objects are equal to one another.

record Pair(int first, int second) {
    @Override
    public String toString() {
        return "(%d, %d)".formatted(first, second);
    }
}

class PairComparator implements Comparator<Pair> {
    @Override
    public int compare(Pair o1, Pair o2) {
        if (o1.first() == o2.first()) {
            return o1.second() - o2.second();
        } else {
            return o1.first() - o2.first();
        }
    }
}
final Pair[] a = {
        new Pair(0, 0), new Pair(0, 1), new Pair(0, 2),
        new Pair(1, 0), new Pair(1, 1), new Pair(1, 2)};

final Pair[] a2 = {
        new Pair(0, 0), new Pair(0, 1), new Pair(0, 2),
        new Pair(1, 0), new Pair(1, 1), new Pair(1, 2)};

final var cmp = new PairComparator();

System.out.println(Arrays.equals(a, a2, cmp)); // true
final Pair[] a = {
        new Pair(0, 0), new Pair(0, 1), new Pair(0, 2),
        new Pair(1, 0), new Pair(1, 1), new Pair(1, 2)};

final Pair[] a2 = {
        new Pair(0, 2), new Pair(1, 0), new Pair(1, 1),
        new Pair(1, 2), new Pair(0, 0), new Pair(0, 1)};

final var cmp = new PairComparator();

System.out.println(Arrays.equals(a, a2, cmp)); // false
final Pair[] a = {new Pair(0, 0), new Pair(0, 0), new Pair(0, 0)};
final Pair[] a2 = {new Pair(0, 0), new Pair(0, 0)};
final var cmp = new PairComparator();

System.out.println(Arrays.equals(a, a2, cmp)); // false

static void fill (boolean[] a, boolean val)

Assigns the specified boolean value to each element of the specified array of booleans.

final var a = new boolean[5];
System.out.println(Arrays.toString(a)); // [false, false, false, false, false]

Arrays.fill(a, true);
System.out.println(Arrays.toString(a)); // [true, true, true, true, true]

static void fill (boolean[] a, int fromIndex, int toIndex, boolean val)

Assigns the specified boolean value to each element of the specified range of the specified array of booleans.

final var a1 = new boolean[4];
final var a2 = new boolean[4];
final var a3 = new boolean[4];
final var a4 = new boolean[4];
System.out.println(Arrays.toString(a1)); // [false, false, false, false]

Arrays.fill(a1, 0, 4, true);
Arrays.fill(a2, 0, 3, true);
Arrays.fill(a3, 0, 2, true);
Arrays.fill(a4, 0, 1, true);

System.out.println(Arrays.toString(a1)); // [true, true, true, true]
System.out.println(Arrays.toString(a2)); // [true, true, true, false]
System.out.println(Arrays.toString(a3)); // [true, true, false, false]
System.out.println(Arrays.toString(a4)); // [true, false, false, false]
final var a1 = new boolean[4];
final var a2 = new boolean[4];
final var a3 = new boolean[4];
final var a4 = new boolean[4];
System.out.println(Arrays.toString(a1)); // [false, false, false, false]

Arrays.fill(a1, 0, 4, true);
Arrays.fill(a2, 1, 4, true);
Arrays.fill(a3, 2, 4, true);
Arrays.fill(a4, 3, 4, true);

System.out.println(Arrays.toString(a1)); // [true, true, true, true]
System.out.println(Arrays.toString(a2)); // [false, true, true, true]
System.out.println(Arrays.toString(a3)); // [false, false, true, true]
System.out.println(Arrays.toString(a4)); // [false, false, false, true]

static void fill (byte[] a, byte val)

Assigns the specified byte value to each element of the specified array of bytes.

This method is equivalent except a type to 、fill(int[] a, int val).

static void fill (byte[] a, int fromIndex, int toIndex, byte val)

Assigns the specified byte value to each element of the specified range of the specified array of bytes.

This method is equivalent except a type to fill(int[] a, int fromIndex, int toIndex, int val).

static void fill (char[] a, char val)

Assigns the specified char value to each element of the specified array of chars.

final var a = new char[5];
System.out.println(Arrays.toString(a)); // [ ,  ,  ,  ,  ]

Arrays.fill(a, 'a');
System.out.println(Arrays.toString(a)); // [a, a, a, a, a]

static void fill (char[] a, int fromIndex, int toIndex, char val)

Assigns the specified char value to each element of the specified range of the specified array of chars.

final var a1 = new char[4];
final var a2 = new char[4];
final var a3 = new char[4];
final var a4 = new char[4];
System.out.println(Arrays.toString(a1)); // [ ,  ,  ,  ]

Arrays.fill(a1, 0, 4, 'a');
Arrays.fill(a2, 0, 3, 'a');
Arrays.fill(a3, 0, 2, 'a');
Arrays.fill(a4, 0, 1, 'a');

System.out.println(Arrays.toString(a1)); // [a, a, a, a]
System.out.println(Arrays.toString(a2)); // [a, a, a,  ]
System.out.println(Arrays.toString(a3)); // [a, a,  ,  ]
System.out.println(Arrays.toString(a4)); // [a,  ,  ,  ]
final var a1 = new char[4];
final var a2 = new char[4];
final var a3 = new char[4];
final var a4 = new char[4];
System.out.println(Arrays.toString(a1)); // [ ,  ,  ,  ]

Arrays.fill(a1, 0, 4, 'a');
Arrays.fill(a2, 1, 4, 'a');
Arrays.fill(a3, 2, 4, 'a');
Arrays.fill(a4, 3, 4, 'a');

System.out.println(Arrays.toString(a1)); // [a, a, a, a]
System.out.println(Arrays.toString(a2)); // [ , a, a, a]
System.out.println(Arrays.toString(a3)); // [ ,  , a, a]
System.out.println(Arrays.toString(a4)); // [ ,  ,  , a]

static void fill (double[] a, double val)

Assigns the specified double value to each element of the specified array of doubles.

This method is equivalent except a type to fill(int[] a, int val).

static void fill (double[] a, int fromIndex, int toIndex, double val)

Assigns the specified double value to each element of the specified range of the specified array of doubles.

This method is equivalent except a type to fill(int[] a, int fromIndex, int toIndex, int val).

static void fill (float[] a, float val)

Assigns the specified float value to each element of the specified array of floats.

This method is equivalent except a type to fill(int[] a, int val).

static void fill (float[] a, int fromIndex, int toIndex, float val)

Assigns the specified float value to each element of the specified range of the specified array of floats.

This method is equivalent except a type to fill(int[] a, int fromIndex, int toIndex, int val).

static void fill (int[] a, int val)

Assigns the specified int value to each element of the specified array of ints.

final var a = new int[5];
System.out.println(Arrays.toString(a)); // [0, 0, 0, 0, 0]

Arrays.fill(a, 9);
System.out.println(Arrays.toString(a)); // [9, 9, 9, 9, 9]

static void fill (int[] a, int fromIndex, int toIndex, int val)

Assigns the specified int value to each element of the specified range of the specified array of ints.

final var a1 = new int[4];
final var a2 = new int[4];
final var a3 = new int[4];
final var a4 = new int[4];
System.out.println(Arrays.toString(a1)); // [0, 0, 0, 0]

Arrays.fill(a1, 0, 4, 9);
Arrays.fill(a2, 0, 3, 9);
Arrays.fill(a3, 0, 2, 9);
Arrays.fill(a4, 0, 1, 9);

System.out.println(Arrays.toString(a1)); // [9, 9, 9, 9]
System.out.println(Arrays.toString(a2)); // [9, 9, 9, 0]
System.out.println(Arrays.toString(a3)); // [9, 9, 0, 0]
System.out.println(Arrays.toString(a4)); // [9, 0, 0, 0]
final var a1 = new int[4];
final var a2 = new int[4];
final var a3 = new int[4];
final var a4 = new int[4];
System.out.println(Arrays.toString(a1)); // [0, 0, 0, 0]

Arrays.fill(a1, 0, 4, 9);
Arrays.fill(a2, 1, 4, 9);
Arrays.fill(a3, 2, 4, 9);
Arrays.fill(a4, 3, 4, 9);

System.out.println(Arrays.toString(a1)); // [9, 9, 9, 9]
System.out.println(Arrays.toString(a2)); // [0, 9, 9, 9]
System.out.println(Arrays.toString(a3)); // [0, 0, 9, 9]
System.out.println(Arrays.toString(a4)); // [0, 0, 0, 9]

static void fill (long[] a, int fromIndex, int toIndex, long val)

Assigns the specified long value to each element of the specified range of the specified array of longs.

This method is equivalent except a type to fill(int[] a, int fromIndex, int toIndex, int val).

static void fill (long[] a, long val)

Assigns the specified long value to each element of the specified array of longs.

This method is equivalent except a type to fill(int[] a, int val).

static void fill (short[] a, int fromIndex, int toIndex, short val)

Assigns the specified short value to each element of the specified range of the specified array of shorts.

This method is equivalent except a type to fill(int[] a, int fromIndex, int toIndex, int val).

static void fill (short[] a, short val)

Assigns the specified short value to each element of the specified array of shorts.

This method is equivalent except a type to fill(int[] a, int val).

static void fill (Object[] a, int fromIndex, int toIndex, Object val)

Assigns the specified Object reference to each element of the specified range of the specified array of Objects.

final var a1 = new String[4];
final var a2 = new String[4];
final var a3 = new String[4];
final var a4 = new String[4];
System.out.println(Arrays.toString(a1)); // [null, null, null, null]

Arrays.fill(a1, 0, 4, "a");
Arrays.fill(a2, 0, 3, "a");
Arrays.fill(a3, 0, 2, "a");
Arrays.fill(a4, 0, 1, "a");

System.out.println(Arrays.toString(a1)); // [a, a, a, a]
System.out.println(Arrays.toString(a2)); // [a, a, a, null]
System.out.println(Arrays.toString(a3)); // [a, a, null, null]
System.out.println(Arrays.toString(a4)); // [a, null, null, null]
final var a1 = new String[4];
final var a2 = new String[4];
final var a3 = new String[4];
final var a4 = new String[4];
System.out.println(Arrays.toString(a1)); // [null, null, null, null]

Arrays.fill(a1, 0, 4, "a");
Arrays.fill(a2, 1, 4, "a");
Arrays.fill(a3, 2, 4, "a");
Arrays.fill(a4, 3, 4, "a");

System.out.println(Arrays.toString(a1)); // [a, a, a, a]
System.out.println(Arrays.toString(a2)); // [null, a, a, a]
System.out.println(Arrays.toString(a3)); // [null, null, a, a]
System.out.println(Arrays.toString(a4)); // [null, null, null, a]

static void fill (Object[] a, Object val)

Assigns the specified Object reference to each element of the specified array of Objects.

final var a = new String[5];
System.out.println(Arrays.toString(a)); // [null, null, null, null, null]

Arrays.fill(a, "a");
System.out.println(Arrays.toString(a)); // [a, a, a, a, a]

static int hashCode (boolean[] a)

Returns a hash code based on the contents of the specified array.

final boolean[] a1 = {true, false, true};
final boolean[] a2 = {true, false, true};

System.out.println(a1 != a2); // true

System.out.println(Arrays.hashCode(a1)); // 1252360
System.out.println(Arrays.hashCode(a2)); // 1252360

System.out.println(a1.hashCode()); // 964039755
System.out.println(a2.hashCode()); // 617590710

static int hashCode (byte[] a)

Returns a hash code based on the contents of the specified array.

This method is equivalent except a type to hashCode(int[] a).

static int hashCode (char[] a)

Returns a hash code based on the contents of the specified array.

final char[] a1 = {'a', 'b', 'c'};
final char[] a2 = {'a', 'b', 'c'};

System.out.println(a1 != a2); // true

System.out.println(Arrays.hashCode(a1)); // 126145
System.out.println(Arrays.hashCode(a2)); // 126145

System.out.println(a1.hashCode()); // 10241554
System.out.println(a2.hashCode()); // 741227068

static int hashCode (double[] a)

Returns a hash code based on the contents of the specified array.

This method is equivalent except a type to hashCode(int[] a).

static int hashCode (float[] a)

Returns a hash code based on the contents of the specified array.

This method is equivalent except a type to hashCode(int[] a).

static int hashCode (int[] a)

Returns a hash code based on the contents of the specified array.

final char[] a1 = {1, 2, 3};
final char[] a2 = {1, 2, 3};

System.out.println(a1 != a2); // true

System.out.println(Arrays.hashCode(a1)); // 30817
System.out.println(Arrays.hashCode(a2)); // 30817

System.out.println(a1.hashCode()); // 636725182
System.out.println(a2.hashCode()); // 2081951401

static int hashCode (long[] a)

Returns a hash code based on the contents of the specified array.

This method is equivalent except a type to hashCode(int[] a).

static int hashCode (short[] a)

Returns a hash code based on the contents of the specified array.

This method is equivalent except a type to hashCode(int[] a).

static int hashCode (Object[] a)

Returns a hash code based on the contents of the specified array.

final String[] a1 = {"aa", "bb", "cc"};
final String[] a2 = {"aa", "bb", "cc"};

System.out.println(a1 != a2); // true

System.out.println(Arrays.hashCode(a1)); // 3113119
System.out.println(Arrays.hashCode(a2)); // 3113119

System.out.println(a1.hashCode()); // 508984302
System.out.println(a2.hashCode()); // 1140084162

static int mismatch (boolean[] a, boolean[] b)

Finds and returns the index of the first mismatch between two boolean arrays, otherwise return -1 if no mismatch is found.

final boolean[] a = {true, true, true};

final boolean[] b1 = {false, false, false};
final boolean[] b2 = {true, false, false};
final boolean[] b3 = {true, true, false};
final boolean[] b4 = {true, true, true};

System.out.println(Arrays.mismatch(a, b1)); // 0
System.out.println(Arrays.mismatch(a, b2)); // 1
System.out.println(Arrays.mismatch(a, b3)); // 2
System.out.println(Arrays.mismatch(a, b4)); // -1

static int mismatch (boolean[] a, int aFromIndex, int aToIndex, boolean[] b, int bFromIndex, int bToIndex)

Finds and returns the relative index of the first mismatch between two boolean arrays over the specified ranges, otherwise return -1 if no mismatch is found.

final boolean[] a = {true, true, true, false, false, false};
final boolean[] b = {true, true, true, true, true, true};

System.out.println(Arrays.mismatch(a, 0, 6, b, 0, 6)); // 3

System.out.println(Arrays.mismatch(a, 0, 5, b, 0, 5)); // 3
System.out.println(Arrays.mismatch(a, 0, 4, b, 0, 4)); // 3
System.out.println(Arrays.mismatch(a, 0, 3, b, 0, 3)); // -1

System.out.println(Arrays.mismatch(a, 1, 6, b, 1, 6)); // 2
System.out.println(Arrays.mismatch(a, 2, 6, b, 2, 6)); // 1
System.out.println(Arrays.mismatch(a, 3, 6, b, 3, 6)); // 0

static int mismatch (byte[] a, byte[] b)

Finds and returns the index of the first mismatch between two byte arrays, otherwise return -1 if no mismatch is found.

This method is equivalent except a type to mismatch(int[] a, int[] b).

static int mismatch (byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)

Finds and returns the relative index of the first mismatch between two byte arrays over the specified ranges, otherwise return -1 if no mismatch is found.

This method is equivalent except a type to mismatch(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static int mismatch (char[] a, char[] b)

Finds and returns the index of the first mismatch between two char arrays, otherwise return -1 if no mismatch is found.

final char[] a = {'a', 'b', 'c'};

final char[] b1 = {'z', 'b', 'c'};
final char[] b2 = {'a', 'z', 'c'};
final char[] b3 = {'a', 'b', 'z'};
final char[] b4 = {'a', 'b', 'c'};

System.out.println(Arrays.mismatch(a, b1)); // 0
System.out.println(Arrays.mismatch(a, b2)); // 1
System.out.println(Arrays.mismatch(a, b3)); // 2
System.out.println(Arrays.mismatch(a, b4)); // -1

static int mismatch (char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex)

Finds and returns the relative index of the first mismatch between two char arrays over the specified ranges, otherwise return -1 if no mismatch is found.

final char[] a = {'a', 'b', 'c', 'd', 'e', 'f'};
final char[] b = {'a', 'b', 'c', 'x', 'y', 'z'};

System.out.println(Arrays.mismatch(a, 0, 6, b, 0, 6)); // 3

System.out.println(Arrays.mismatch(a, 0, 5, b, 0, 5)); // 3
System.out.println(Arrays.mismatch(a, 0, 4, b, 0, 4)); // 3
System.out.println(Arrays.mismatch(a, 0, 3, b, 0, 3)); // -1

System.out.println(Arrays.mismatch(a, 1, 6, b, 1, 6)); // 2
System.out.println(Arrays.mismatch(a, 2, 6, b, 2, 6)); // 1
System.out.println(Arrays.mismatch(a, 3, 6, b, 3, 6)); // 0

static int mismatch (double[] a, double[] b)

Finds and returns the index of the first mismatch between two double arrays, otherwise return -1 if no mismatch is found.

This method is equivalent except a type to mismatch(int[] a, int[] b).

static int mismatch (double[] a, int aFromIndex, int aToIndex, double[] b, int bFromIndex, int bToIndex)

Finds and returns the relative index of the first mismatch between two double arrays over the specified ranges, otherwise return -1 if no mismatch is found.

This method is equivalent except a type to mismatch(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static int mismatch (float[] a, float[] b)

Finds and returns the index of the first mismatch between two float arrays, otherwise return -1 if no mismatch is found.

This method is equivalent except a type to mismatch(int[] a, int[] b).

static int mismatch (float[] a, int aFromIndex, int aToIndex, float[] b, int bFromIndex, int bToIndex)

Finds and returns the relative index of the first mismatch between two float arrays over the specified ranges, otherwise return -1 if no mismatch is found.

This method is equivalent except a type to mismatch(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static int mismatch (int[] a, int[] b)

Finds and returns the index of the first mismatch between two int arrays, otherwise return -1 if no mismatch is found.

final int[] a = {0, 1, 2};

final int[] b1 = {9, 1, 2};
final int[] b2 = {0, 9, 2};
final int[] b3 = {0, 1, 9};
final int[] b4 = {0, 1, 2};

System.out.println(Arrays.mismatch(a, b1)); // 0
System.out.println(Arrays.mismatch(a, b2)); // 1
System.out.println(Arrays.mismatch(a, b3)); // 2
System.out.println(Arrays.mismatch(a, b4)); // -1

static int mismatch (int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)

Finds and returns the relative index of the first mismatch between two int arrays over the specified ranges, otherwise return -1 if no mismatch is found.

final int[] a = {0, 1, 2, 3, 4, 5};
final int[] b = {0, 1, 2, 30, 40, 50};

System.out.println(Arrays.mismatch(a, 0, 6, b, 0, 6)); // 3

System.out.println(Arrays.mismatch(a, 0, 5, b, 0, 5)); // 3
System.out.println(Arrays.mismatch(a, 0, 4, b, 0, 4)); // 3
System.out.println(Arrays.mismatch(a, 0, 3, b, 0, 3)); // -1

System.out.println(Arrays.mismatch(a, 1, 6, b, 1, 6)); // 2
System.out.println(Arrays.mismatch(a, 2, 6, b, 2, 6)); // 1
System.out.println(Arrays.mismatch(a, 3, 6, b, 3, 6)); // 0

static int mismatch (long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex)

Finds and returns the relative index of the first mismatch between two long arrays over the specified ranges, otherwise return -1 if no mismatch is found.

This method is equivalent except a type to mismatch(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static int mismatch (long[] a, long[] b)

Finds and returns the index of the first mismatch between two long arrays, otherwise return -1 if no mismatch is found.

This method is equivalent except a type to mismatch(int[] a, int[] b).

static int mismatch (short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex)

Finds and returns the relative index of the first mismatch between two short arrays over the specified ranges, otherwise return -1 if no mismatch is found.

This method is equivalent except a type to mismatch(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex).

static int mismatch (short[] a, short[] b)

Finds and returns the index of the first mismatch between two short arrays, otherwise return -1 if no mismatch is found.

This method is equivalent except a type to mismatch(int[] a, int[] b).

static int mismatch (Object[] a, int aFromIndex, int aToIndex, Object[] b, int bFromIndex, int bToIndex)

Finds and returns the relative index of the first mismatch between two Object arrays over the specified ranges, otherwise return -1 if no mismatch is found.

final String[] a = {"a", "b", "c", "d", "e", "f"};
final String[] b = {"a", "b", "c", "x", "y", "z"};

System.out.println(Arrays.mismatch(a, 0, 6, b, 0, 6)); // 3

System.out.println(Arrays.mismatch(a, 0, 5, b, 0, 5)); // 3
System.out.println(Arrays.mismatch(a, 0, 4, b, 0, 4)); // 3
System.out.println(Arrays.mismatch(a, 0, 3, b, 0, 3)); // -1

System.out.println(Arrays.mismatch(a, 1, 6, b, 1, 6)); // 2
System.out.println(Arrays.mismatch(a, 2, 6, b, 2, 6)); // 1
System.out.println(Arrays.mismatch(a, 3, 6, b, 3, 6)); // 0

static int mismatch (Object[] a, Object[] b)

Finds and returns the index of the first mismatch between two Object arrays, otherwise return -1 if no mismatch is found.

final String[] a = {"a", "b", "c"};

final String[] b1 = {"z", "b", "c"};
final String[] b2 = {"a", "z", "c"};
final String[] b3 = {"a", "b", "z"};
final String[] b4 = {"a", "b", "c"};

System.out.println(Arrays.mismatch(a, b1)); // 0
System.out.println(Arrays.mismatch(a, b2)); // 1
System.out.println(Arrays.mismatch(a, b3)); // 2
System.out.println(Arrays.mismatch(a, b4)); // -1

static <T> int mismatch (T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp)

Finds and returns the relative index of the first mismatch between two Object arrays over the specified ranges, otherwise return -1 if no mismatch is found.

record Pair(int first, int second) {
    @Override
    public String toString() {
        return "(%d, %d)".formatted(first, second);
    }
}

class PairComparator implements Comparator<Pair> {
    @Override
    public int compare(Pair o1, Pair o2) {
        if (o1.first() == o2.first()) {
            return o1.second() - o2.second();
        } else {
            return o1.first() - o2.first();
        }
    }
}
final Pair[] a = {
        new Pair(0, 1), new Pair(0, 2), new Pair(0, 3),
        new Pair(0, 4), new Pair(0, 5), new Pair(0, 6)};

final Pair[] b = {
        new Pair(0, 1), new Pair(0, 2), new Pair(0, 3),
        new Pair(1, 1), new Pair(1, 2), new Pair(1, 3)};

final var cmp = new PairComparator();

System.out.println(Arrays.mismatch(a, 0, 6, b, 0, 6, cmp)); // 3

System.out.println(Arrays.mismatch(a, 0, 5, b, 0, 5, cmp)); // 3
System.out.println(Arrays.mismatch(a, 0, 4, b, 0, 4, cmp)); // 3
System.out.println(Arrays.mismatch(a, 0, 3, b, 0, 3, cmp)); // -1

System.out.println(Arrays.mismatch(a, 1, 6, b, 1, 6, cmp)); // 2
System.out.println(Arrays.mismatch(a, 2, 6, b, 2, 6, cmp)); // 1
System.out.println(Arrays.mismatch(a, 3, 6, b, 3, 6, cmp)); // 0

static <T> int mismatch (T[] a, T[] b, Comparator<? super T> cmp)

Finds and returns the index of the first mismatch between two Object arrays, otherwise return -1 if no mismatch is found.

record Pair(int first, int second) {
    @Override
    public String toString() {
        return "(%d, %d)".formatted(first, second);
    }
}

class PairComparator implements Comparator<Pair> {
    @Override
    public int compare(Pair o1, Pair o2) {
        if (o1.first() == o2.first()) {
            return o1.second() - o2.second();
        } else {
            return o1.first() - o2.first();
        }
    }
}
final Pair[] a = {new Pair(0, 1), new Pair(0, 2), new Pair(0, 3)};

final Pair[] b1 = {new Pair(9, 9), new Pair(0, 2), new Pair(0, 3)};
final Pair[] b2 = {new Pair(0, 1), new Pair(9, 9), new Pair(0, 3)};
final Pair[] b3 = {new Pair(0, 1), new Pair(0, 2), new Pair(9, 9)};
final Pair[] b4 = {new Pair(0, 1), new Pair(0, 2), new Pair(0, 3)};

final var cmp = new PairComparator();

System.out.println(Arrays.mismatch(a, b1, cmp)); // 0
System.out.println(Arrays.mismatch(a, b2, cmp)); // 1
System.out.println(Arrays.mismatch(a, b3, cmp)); // 2
System.out.println(Arrays.mismatch(a, b4, cmp)); // -1

static void parallelPrefix (double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op)

Performs parallelPrefix(double[], DoubleBinaryOperator) for the given subrange of the array.

This method is equivalent except a type to parallelPrefix(int[] array, int fromIndex, int toIndex, IntBinaryOperator op).

static void parallelPrefix (double[] array, DoubleBinaryOperator op)

Cumulates, in parallel, each element of the given array in place, using the supplied function.

This method is equivalent except a type to parallelPrefix(int[] array, IntBinaryOperator op).

static void parallelPrefix (int[] array, int fromIndex, int toIndex, IntBinaryOperator op)

Performs parallelPrefix(int[], IntBinaryOperator) for the given subrange of the array.

final int[] array = {1, 2, 3, 4, 5, 6};

System.out.println("array : " + Arrays.toString(Arrays.copyOfRange(array, 2, 5)));

Arrays.parallelPrefix(array, 2, 5, (left, right) -> {
    System.out.println("left = %d : right = %d".formatted(left, right));
    return left + right;
});

System.out.println("array : " + Arrays.toString(array));

// Result
// ↓
//array : [3, 4, 5]
//left = 3 : right = 4
//left = 7 : right = 5
//array : [1, 2, 3, 7, 12, 6]

static void parallelPrefix (int[] array, IntBinaryOperator op)

Cumulates, in parallel, each element of the given array in place, using the supplied function.

final int[] array = {1, 2, 3, 4};

Arrays.parallelPrefix(array, (left, right) -> {
    System.out.println("left = %d : right = %d".formatted(left, right));
    return left + right;
});

System.out.println("array : " + Arrays.toString(array));

// Result
// ↓
//left = 1 : right = 2
//left = 3 : right = 3
//left = 6 : right = 4
//array : [1, 3, 6, 10]

static void parallelPrefix (long[] array, int fromIndex, int toIndex, LongBinaryOperator op)

Performs parallelPrefix(long[], LongBinaryOperator) for the given subrange of the array.

This method is equivalent except a type to parallelPrefix(int[] array, int fromIndex, int toIndex, IntBinaryOperator op).

static void parallelPrefix (long[] array, LongBinaryOperator op)

Cumulates, in parallel, each element of the given array in place, using the supplied function.

This method is equivalent except a type to parallelPrefix(int[] array, IntBinaryOperator op).

static <T> void parallelPrefix (T[] array, int fromIndex, int toIndex, BinaryOperator<T> op)

Performs parallelPrefix(Object[], BinaryOperator) for the given subrange of the array.

final String[] array = {"a", "b", "c", "d", "e", "f"};

System.out.println("array : " + Arrays.toString(Arrays.copyOfRange(array, 2, 5)));

Arrays.parallelPrefix(array, 2, 5, (left, right) -> {
    System.out.println("left = %s : right = %s".formatted(left, right));
    return left + right;
});

System.out.println("array : " + Arrays.toString(array));

// Result
// ↓
//array : [c, d, e]
//left = c : right = d
//left = cd : right = e
//array : [a, b, c, cd, cde, f]

static <T> void parallelPrefix (T[] array, BinaryOperator<T> op)

Cumulates, in parallel, each element of the given array in place, using the supplied function.

final String[] array = {"a", "b", "c", "d"};

Arrays.parallelPrefix(array, (left, right) -> {
    System.out.println("left = %s : right = %s".formatted(left, right));
    return left + right;
});

System.out.println("array : " + Arrays.toString(array));

// Result
// ↓
//left = a : right = b
//left = ab : right = c
//left = abc : right = d
//array : [a, ab, abc, abcd]

static void parallelSetAll (double[] array, IntToDoubleFunction generator)

Set all elements of the specified array, in parallel, using the provided generator function to compute each element.

This method is equivalent except a type to parallelSetAll(int[] array, IntUnaryOperator generator).

static void parallelSetAll (int[] array, IntUnaryOperator generator)

Set all elements of the specified array, in parallel, using the provided generator function to compute each element.

final var array = new int[5];

System.out.println("before : " + Arrays.toString(array));

Arrays.parallelSetAll(array, index -> {
    final var id = Thread.currentThread().getId();
    System.out.println("thread id = %d : index = %d".formatted(id, index));
    return index * 2;
});

System.out.println("after : " + Arrays.toString(array));

// Result
// ↓
//before : [0, 0, 0, 0, 0]
//thread id = 16 : index = 2
//thread id = 21 : index = 0
//thread id = 23 : index = 3
//thread id = 20 : index = 4
//thread id = 19 : index = 1
//after : [0, 2, 4, 6, 8]

static void parallelSetAll (long[] array, IntToLongFunction generator)

Set all elements of the specified array, in parallel, using the provided generator function to compute each element.

This method is equivalent except a type to parallelSetAll(int[] array, IntUnaryOperator generator).

static <T> void parallelSetAll (T[] array, IntFunction<? extends T> generator)

Set all elements of the specified array, in parallel, using the provided generator function to compute each element.

final var array = new String[5];

System.out.println("before : " + Arrays.toString(array));

Arrays.parallelSetAll(array, index -> {
    final var id = Thread.currentThread().getId();
    System.out.println("thread id = %d : index = %d".formatted(id, index));
    return "a".repeat(index + 1);
});

System.out.println("after : " + Arrays.toString(array));

// Result
// ↓
//before : [null, null, null, null, null]
//thread id = 16 : index = 2
//thread id = 20 : index = 4
//thread id = 19 : index = 1
//thread id = 21 : index = 3
//thread id = 22 : index = 0
//after : [a, aa, aaa, aaaa, aaaaa]

static void parallelSort (byte[] a)

Sorts the specified array into ascending numerical order.

This method is equivalent except a type to parallelSort(int[] a).

static void parallelSort (byte[] a, int fromIndex, int toIndex)

Sorts the specified range of the array into ascending numerical order.

This method is equivalent except a type to parallelSort(int[] a, int fromIndex, int toIndex).

static void parallelSort (char[] a)

Sorts the specified array into ascending numerical order.

final char[] a = {'d', 'c', 'b', 'a'};

Arrays.parallelSort(a);

System.out.println(Arrays.toString(a)); // [a, b, c, d]

static void parallelSort (char[] a, int fromIndex, int toIndex)

Sorts the specified range of the array into ascending numerical order.

final char[] a = {'d', 'c', 'b', 'a'};

{
    final char[] a1 = a.clone();
    final char[] a2 = a.clone();
    final char[] a3 = a.clone();
    final char[] a4 = a.clone();

    Arrays.parallelSort(a1, 0, 4);
    Arrays.parallelSort(a2, 0, 3);
    Arrays.parallelSort(a3, 0, 2);
    Arrays.parallelSort(a4, 0, 1);

    System.out.println(Arrays.toString(a1)); // [a, b, c, d]
    System.out.println(Arrays.toString(a2)); // [b, c, d, a]
    System.out.println(Arrays.toString(a3)); // [c, d, b, a]
    System.out.println(Arrays.toString(a4)); // [d, c, b, a]
}

{
    final char[] a1 = a.clone();
    final char[] a2 = a.clone();
    final char[] a3 = a.clone();
    final char[] a4 = a.clone();

    Arrays.parallelSort(a1, 0, 4);
    Arrays.parallelSort(a2, 1, 4);
    Arrays.parallelSort(a3, 2, 4);
    Arrays.parallelSort(a4, 3, 4);

    System.out.println(Arrays.toString(a1)); // [a, b, c, d]
    System.out.println(Arrays.toString(a2)); // [d, a, b, c]
    System.out.println(Arrays.toString(a3)); // [d, c, a, b]
    System.out.println(Arrays.toString(a4)); // [d, c, b, a]
}

static void parallelSort (double[] a)

Sorts the specified array into ascending numerical order.

This method is equivalent except a type to parallelSort(int[] a).

static void parallelSort (double[] a, int fromIndex, int toIndex)

Sorts the specified range of the array into ascending numerical order.

This method is equivalent except a type to parallelSort(int[] a, int fromIndex, int toIndex).

static void parallelSort (float[] a)

Sorts the specified array into ascending numerical order.

This method is equivalent except a type to parallelSort(int[] a).

static void parallelSort (float[] a, int fromIndex, int toIndex)

Sorts the specified range of the array into ascending numerical order.

This method is equivalent except a type to parallelSort(int[] a, int fromIndex, int toIndex).

static void parallelSort (int[] a)

Sorts the specified array into ascending numerical order.

final int[] a = {4, 3, 2, 1};

Arrays.parallelSort(a);

System.out.println(Arrays.toString(a)); // [1, 2, 3, 4]

static void parallelSort (int[] a, int fromIndex, int toIndex)

Sorts the specified range of the array into ascending numerical order.

final int[] a = {4, 3, 2, 1};

{
    final int[] a1 = a.clone();
    final int[] a2 = a.clone();
    final int[] a3 = a.clone();
    final int[] a4 = a.clone();

    Arrays.parallelSort(a1, 0, 4);
    Arrays.parallelSort(a2, 0, 3);
    Arrays.parallelSort(a3, 0, 2);
    Arrays.parallelSort(a4, 0, 1);

    System.out.println(Arrays.toString(a1)); // [1, 2, 3, 4]
    System.out.println(Arrays.toString(a2)); // [2, 3, 4, 1]
    System.out.println(Arrays.toString(a3)); // [3, 4, 2, 1]
    System.out.println(Arrays.toString(a4)); // [4, 3, 2, 1]
}

{
    final int[] a1 = a.clone();
    final int[] a2 = a.clone();
    final int[] a3 = a.clone();
    final int[] a4 = a.clone();

    Arrays.parallelSort(a1, 0, 4);
    Arrays.parallelSort(a2, 1, 4);
    Arrays.parallelSort(a3, 2, 4);
    Arrays.parallelSort(a4, 3, 4);

    System.out.println(Arrays.toString(a1)); // [1, 2, 3, 4]
    System.out.println(Arrays.toString(a2)); // [4, 1, 2, 3]
    System.out.println(Arrays.toString(a3)); // [4, 3, 1, 2]
    System.out.println(Arrays.toString(a4)); // [4, 3, 2, 1]
}

static void parallelSort (long[] a)

Sorts the specified array into ascending numerical order.

This method is equivalent except a type to parallelSort(int[] a).

static void parallelSort (long[] a, int fromIndex, int toIndex)

Sorts the specified range of the array into ascending numerical order.

This method is equivalent except a type to parallelSort(int[] a, int fromIndex, int toIndex).

static void parallelSort (short[] a)

Sorts the specified array into ascending numerical order.

This method is equivalent except a type to parallelSort(int[] a).

static void parallelSort (short[] a, int fromIndex, int toIndex)

Sorts the specified range of the array into ascending numerical order.

This method is equivalent except a type to parallelSort(int[] a, int fromIndex, int toIndex).

static <T extends Comparable<? super T>> void parallelSort (T[] a)

Sorts the specified array of objects into ascending order, according to the natural ordering of its elements.

final String[] a = {"d", "c", "b", "a"};

Arrays.parallelSort(a);

System.out.println(Arrays.toString(a)); // [a, b, c, d]

static <T extends Comparable<? super T>> void parallelSort (T[] a, int fromIndex, int toIndex)

Sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements.

final String[] a = {"d", "c", "b", "a"};

{
    final String[] a1 = a.clone();
    final String[] a2 = a.clone();
    final String[] a3 = a.clone();
    final String[] a4 = a.clone();

    Arrays.parallelSort(a1, 0, 4);
    Arrays.parallelSort(a2, 0, 3);
    Arrays.parallelSort(a3, 0, 2);
    Arrays.parallelSort(a4, 0, 1);

    System.out.println(Arrays.toString(a1)); // [a, b, c, d]
    System.out.println(Arrays.toString(a2)); // [b, c, d, a]
    System.out.println(Arrays.toString(a3)); // [c, d, b, a]
    System.out.println(Arrays.toString(a4)); // [d, c, b, a]
}

{
    final String[] a1 = a.clone();
    final String[] a2 = a.clone();
    final String[] a3 = a.clone();
    final String[] a4 = a.clone();

    Arrays.parallelSort(a1, 0, 4);
    Arrays.parallelSort(a2, 1, 4);
    Arrays.parallelSort(a3, 2, 4);
    Arrays.parallelSort(a4, 3, 4);

    System.out.println(Arrays.toString(a1)); // [a, b, c, d]
    System.out.println(Arrays.toString(a2)); // [d, a, b, c]
    System.out.println(Arrays.toString(a3)); // [d, c, a, b]
    System.out.println(Arrays.toString(a4)); // [d, c, b, a]
}

static <T> void parallelSort (T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp)

Sorts the specified range of the specified array of objects according to the order induced by the specified comparator.

record Pair(int first, int second) {
    @Override
    public String toString() {
        return "(%d, %d)".formatted(first, second);
    }
}

class PairComparator implements Comparator<Pair> {
    @Override
    public int compare(Pair o1, Pair o2) {
        if (o1.first() == o2.first()) {
            return o1.second() - o2.second();
        } else {
            return o1.first() - o2.first();
        }
    }
}
final Pair[] a = {
        new Pair(2, 2), new Pair(2, 1),
        new Pair(1, 2), new Pair(1, 1)};

final var cmp = new PairComparator();

{
    final Pair[] a1 = a.clone();
    final Pair[] a2 = a.clone();
    final Pair[] a3 = a.clone();
    final Pair[] a4 = a.clone();

    Arrays.parallelSort(a1, 0, 4, cmp);
    Arrays.parallelSort(a2, 0, 3, cmp);
    Arrays.parallelSort(a3, 0, 2, cmp);
    Arrays.parallelSort(a4, 0, 1, cmp);

    System.out.println(Arrays.toString(a1)); // [(1, 1), (1, 2), (2, 1), (2, 2)]
    System.out.println(Arrays.toString(a2)); // [(1, 2), (2, 1), (2, 2), (1, 1)]
    System.out.println(Arrays.toString(a3)); // [(2, 1), (2, 2), (1, 2), (1, 1)]
    System.out.println(Arrays.toString(a4)); // [(2, 2), (2, 1), (1, 2), (1, 1)]
}

{
    final Pair[] a1 = a.clone();
    final Pair[] a2 = a.clone();
    final Pair[] a3 = a.clone();
    final Pair[] a4 = a.clone();

    Arrays.parallelSort(a1, 0, 4, cmp);
    Arrays.parallelSort(a2, 1, 4, cmp);
    Arrays.parallelSort(a3, 2, 4, cmp);
    Arrays.parallelSort(a4, 3, 4, cmp);

    System.out.println(Arrays.toString(a1)); // [(1, 1), (1, 2), (2, 1), (2, 2)]
    System.out.println(Arrays.toString(a2)); // [(2, 2), (1, 1), (1, 2), (2, 1)]
    System.out.println(Arrays.toString(a3)); // [(2, 2), (2, 1), (1, 1), (1, 2)]
    System.out.println(Arrays.toString(a4)); // [(2, 2), (2, 1), (1, 2), (1, 1)]
}

static <T> void parallelSort (T[] a, Comparator<? super T> cmp)

Sorts the specified array of objects according to the order induced by the specified comparator.

record Pair(int first, int second) {
    @Override
    public String toString() {
        return "(%d, %d)".formatted(first, second);
    }
}

class PairComparator implements Comparator<Pair> {
    @Override
    public int compare(Pair o1, Pair o2) {
        if (o1.first() == o2.first()) {
            return o1.second() - o2.second();
        } else {
            return o1.first() - o2.first();
        }
    }
}
final Pair[] a = {
        new Pair(2, 2), new Pair(2, 1),
        new Pair(1, 2), new Pair(1, 1)};

final var cmp = new PairComparator();

Arrays.parallelSort(a, cmp);

System.out.println(Arrays.toString(a)); // [(1, 1), (1, 2), (2, 1), (2, 2)]

static void setAll (double[] array, IntToDoubleFunction generator)

Set all elements of the specified array, using the provided generator function to compute each element.

This method is equivalent except a type to setAll(int[] array, IntUnaryOperator generator).

static void setAll (int[] array, IntUnaryOperator generator)

Set all elements of the specified array, using the provided generator function to compute each element.

final var array = new int[5];

System.out.println("before : " + Arrays.toString(array));

Arrays.setAll(array, index -> {
    System.out.println("index = %d".formatted(index));
    return index * 2;
});

System.out.println("after : " + Arrays.toString(array));

// Result
// ↓
//before : [0, 0, 0, 0, 0]
//index = 0
//index = 1
//index = 2
//index = 3
//index = 4
//after : [0, 2, 4, 6, 8]

static void setAll (long[] array, IntToLongFunction generator)

Set all elements of the specified array, using the provided generator function to compute each element.

This method is equivalent except a type to setAll(int[] array, IntUnaryOperator generator).

static <T> void setAll (T[] array, IntFunction<? extends T> generator)

Set all elements of the specified array, using the provided generator function to compute each element.

final var array = new String[5];

System.out.println("before : " + Arrays.toString(array));

Arrays.setAll(array, index -> {
    System.out.println("index = %d".formatted(index));
    return "a".repeat(index + 1);
});

System.out.println("after : " + Arrays.toString(array));

// Result
// ↓
//before : [null, null, null, null, null]
//index = 0
//index = 1
//index = 2
//index = 3
//index = 4
//after : [a, aa, aaa, aaaa, aaaaa]

static void sort (byte[] a)

Sorts the specified array into ascending numerical order.

This method is equivalent except a type to sort(int[] a).

static void sort (byte[] a, int fromIndex, int toIndex)

Sorts the specified range of the array into ascending order.

This method is equivalent except a type to sort(int[] a, int fromIndex, int toIndex).

static void sort (char[] a)

Sorts the specified array into ascending numerical order.

final char[] a = {'d', 'c', 'b', 'a'};

Arrays.sort(a);

System.out.println(Arrays.toString(a)); // [a, b, c, d]

static void sort (char[] a, int fromIndex, int toIndex)

Sorts the specified range of the array into ascending order.

final char[] a = {'d', 'c', 'b', 'a'};

{
    final char[] a1 = a.clone();
    final char[] a2 = a.clone();
    final char[] a3 = a.clone();
    final char[] a4 = a.clone();

    Arrays.sort(a1, 0, 4);
    Arrays.sort(a2, 0, 3);
    Arrays.sort(a3, 0, 2);
    Arrays.sort(a4, 0, 1);

    System.out.println(Arrays.toString(a1)); // [a, b, c, d]
    System.out.println(Arrays.toString(a2)); // [b, c, d, a]
    System.out.println(Arrays.toString(a3)); // [c, d, b, a]
    System.out.println(Arrays.toString(a4)); // [d, c, b, a]
}

{
    final char[] a1 = a.clone();
    final char[] a2 = a.clone();
    final char[] a3 = a.clone();
    final char[] a4 = a.clone();

    Arrays.sort(a1, 0, 4);
    Arrays.sort(a2, 1, 4);
    Arrays.sort(a3, 2, 4);
    Arrays.sort(a4, 3, 4);

    System.out.println(Arrays.toString(a1)); // [a, b, c, d]
    System.out.println(Arrays.toString(a2)); // [d, a, b, c]
    System.out.println(Arrays.toString(a3)); // [d, c, a, b]
    System.out.println(Arrays.toString(a4)); // [d, c, b, a]
}

static void sort (double[] a)

Sorts the specified array into ascending numerical order.

This method is equivalent except a type to sort(int[] a).

static void sort (double[] a, int fromIndex, int toIndex)

Sorts the specified range of the array into ascending order.

This method is equivalent except a type to sort(int[] a, int fromIndex, int toIndex).

static void sort (float[] a)

Sorts the specified array into ascending numerical order.

This method is equivalent except a type to sort(int[] a).

static void sort (float[] a, int fromIndex, int toIndex)

Sorts the specified range of the array into ascending order.

This method is equivalent except a type to sort(int[] a, int fromIndex, int toIndex).

static void sort (int[] a)

Sorts the specified array into ascending numerical order.

final int[] a = {4, 3, 2, 1};

Arrays.sort(a);

System.out.println(Arrays.toString(a)); // [1, 2, 3, 4]

static void sort (int[] a, int fromIndex, int toIndex)

Sorts the specified range of the array into ascending order.

final int[] a = {4, 3, 2, 1};

{
    final int[] a1 = a.clone();
    final int[] a2 = a.clone();
    final int[] a3 = a.clone();
    final int[] a4 = a.clone();

    Arrays.sort(a1, 0, 4);
    Arrays.sort(a2, 0, 3);
    Arrays.sort(a3, 0, 2);
    Arrays.sort(a4, 0, 1);

    System.out.println(Arrays.toString(a1)); // [1, 2, 3, 4]
    System.out.println(Arrays.toString(a2)); // [2, 3, 4, 1]
    System.out.println(Arrays.toString(a3)); // [3, 4, 2, 1]
    System.out.println(Arrays.toString(a4)); // [4, 3, 2, 1]
}

{
    final int[] a1 = a.clone();
    final int[] a2 = a.clone();
    final int[] a3 = a.clone();
    final int[] a4 = a.clone();

    Arrays.sort(a1, 0, 4);
    Arrays.sort(a2, 1, 4);
    Arrays.sort(a3, 2, 4);
    Arrays.sort(a4, 3, 4);

    System.out.println(Arrays.toString(a1)); // [1, 2, 3, 4]
    System.out.println(Arrays.toString(a2)); // [4, 1, 2, 3]
    System.out.println(Arrays.toString(a3)); // [4, 3, 1, 2]
    System.out.println(Arrays.toString(a4)); // [4, 3, 2, 1]
}

static void sort (long[] a)

Sorts the specified array into ascending numerical order.

This method is equivalent except a type to sort(int[] a).

static void sort (long[] a, int fromIndex, int toIndex)

Sorts the specified range of the array into ascending order.

This method is equivalent except a type to sort(int[] a, int fromIndex, int toIndex).

static void sort (short[] a)

Sorts the specified array into ascending numerical order.

This method is equivalent except a type to sort(int[] a).

static void sort (short[] a, int fromIndex, int toIndex)

Sorts the specified range of the array into ascending order.

This method is equivalent except a type to sort(int[] a, int fromIndex, int toIndex).

static void sort (Object[] a)

Sorts the specified array of objects into ascending order, according to the natural ordering of its elements.

final String[] a = {"d", "c", "b", "a"};

Arrays.sort(a);

System.out.println(Arrays.toString(a)); // [a, b, c, d]

static void sort (Object[] a, int fromIndex, int toIndex)

Sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements.

final String[] a = {"d", "c", "b", "a"};

{
    final String[] a1 = a.clone();
    final String[] a2 = a.clone();
    final String[] a3 = a.clone();
    final String[] a4 = a.clone();

    Arrays.sort(a1, 0, 4);
    Arrays.sort(a2, 0, 3);
    Arrays.sort(a3, 0, 2);
    Arrays.sort(a4, 0, 1);

    System.out.println(Arrays.toString(a1)); // [a, b, c, d]
    System.out.println(Arrays.toString(a2)); // [b, c, d, a]
    System.out.println(Arrays.toString(a3)); // [c, d, b, a]
    System.out.println(Arrays.toString(a4)); // [d, c, b, a]
}

{
    final String[] a1 = a.clone();
    final String[] a2 = a.clone();
    final String[] a3 = a.clone();
    final String[] a4 = a.clone();

    Arrays.sort(a1, 0, 4);
    Arrays.sort(a2, 1, 4);
    Arrays.sort(a3, 2, 4);
    Arrays.sort(a4, 3, 4);

    System.out.println(Arrays.toString(a1)); // [a, b, c, d]
    System.out.println(Arrays.toString(a2)); // [d, a, b, c]
    System.out.println(Arrays.toString(a3)); // [d, c, a, b]
    System.out.println(Arrays.toString(a4)); // [d, c, b, a]
}

static <T> void sort (T[] a, int fromIndex, int toIndex, Comparator<? super T> c)

Sorts the specified range of the specified array of objects according to the order induced by the specified comparator.

record Pair(int first, int second) {
    @Override
    public String toString() {
        return "(%d, %d)".formatted(first, second);
    }
}

class PairComparator implements Comparator<Pair> {
    @Override
    public int compare(Pair o1, Pair o2) {
        if (o1.first() == o2.first()) {
            return o1.second() - o2.second();
        } else {
            return o1.first() - o2.first();
        }
    }
}
final Pair[] a = {
        new Pair(2, 2), new Pair(2, 1),
        new Pair(1, 2), new Pair(1, 1)};

final var c = new PairComparator();

{
    final Pair[] a1 = a.clone();
    final Pair[] a2 = a.clone();
    final Pair[] a3 = a.clone();
    final Pair[] a4 = a.clone();

    Arrays.sort(a1, 0, 4, c);
    Arrays.sort(a2, 0, 3, c);
    Arrays.sort(a3, 0, 2, c);
    Arrays.sort(a4, 0, 1, c);

    System.out.println(Arrays.toString(a1)); // [(1, 1), (1, 2), (2, 1), (2, 2)]
    System.out.println(Arrays.toString(a2)); // [(1, 2), (2, 1), (2, 2), (1, 1)]
    System.out.println(Arrays.toString(a3)); // [(2, 1), (2, 2), (1, 2), (1, 1)]
    System.out.println(Arrays.toString(a4)); // [(2, 2), (2, 1), (1, 2), (1, 1)]
}

{
    final Pair[] a1 = a.clone();
    final Pair[] a2 = a.clone();
    final Pair[] a3 = a.clone();
    final Pair[] a4 = a.clone();

    Arrays.sort(a1, 0, 4, c);
    Arrays.sort(a2, 1, 4, c);
    Arrays.sort(a3, 2, 4, c);
    Arrays.sort(a4, 3, 4, c);

    System.out.println(Arrays.toString(a1)); // [(1, 1), (1, 2), (2, 1), (2, 2)]
    System.out.println(Arrays.toString(a2)); // [(2, 2), (1, 1), (1, 2), (2, 1)]
    System.out.println(Arrays.toString(a3)); // [(2, 2), (2, 1), (1, 1), (1, 2)]
    System.out.println(Arrays.toString(a4)); // [(2, 2), (2, 1), (1, 2), (1, 1)]
}

static <T> void sort (T[] a, Comparator<? super T> c)

Sorts the specified array of objects according to the order induced by the specified comparator.

record Pair(int first, int second) {
    @Override
    public String toString() {
        return "(%d, %d)".formatted(first, second);
    }
}

class PairComparator implements Comparator<Pair> {
    @Override
    public int compare(Pair o1, Pair o2) {
        if (o1.first() == o2.first()) {
            return o1.second() - o2.second();
        } else {
            return o1.first() - o2.first();
        }
    }
}
final Pair[] a = {
        new Pair(2, 2), new Pair(2, 1),
        new Pair(1, 2), new Pair(1, 1)};

final var c = new PairComparator();

Arrays.sort(a, c);

System.out.println(Arrays.toString(a)); // [(1, 1), (1, 2), (2, 1), (2, 2)]

static Spliterator.OfDouble spliterator (double[] array)

Returns a Spliterator.OfDouble covering all of the specified array.

This method is equivalent except a type to spliterator(int[] array).

static Spliterator.OfDouble spliterator (double[] array, int startInclusive, int endExclusive)

Returns a Spliterator.OfDouble covering the specified range of the specified array.

This method is equivalent except a type to spliterator(int[] array, int startInclusive, int endExclusive).

static Spliterator.OfInt spliterator (int[] array)

Returns a Spliterator.OfInt covering all of the specified array.

final int[] array = {1, 2, 3, 4};

final var spliterator = Arrays.spliterator(array);

spliterator.forEachRemaining((IntConsumer) value -> {
    System.out.print(value + ",");
});
System.out.println();

// Result
// ↓
//1,2,3,4,

static Spliterator.OfInt spliterator (int[] array, int startInclusive, int endExclusive)

Returns a Spliterator.OfInt covering the specified range of the specified array.

final int[] array = {1, 2, 3, 4};

Arrays.spliterator(array, 0, 4)
        .forEachRemaining((IntConsumer) value -> System.out.print(value + ","));
System.out.println();

Arrays.spliterator(array, 0, 3)
        .forEachRemaining((IntConsumer) value -> System.out.print(value + ","));
System.out.println();

Arrays.spliterator(array, 0, 2)
        .forEachRemaining((IntConsumer) value -> System.out.print(value + ","));
System.out.println();

Arrays.spliterator(array, 0, 1)
        .forEachRemaining((IntConsumer) value -> System.out.print(value + ","));
System.out.println();

// Result
// ↓
//1,2,3,4,
//1,2,3,
//1,2,
//1,
final int[] array = {1, 2, 3, 4};

Arrays.spliterator(array, 0, 4).
        forEachRemaining((IntConsumer) value -> System.out.print(value + ","));
System.out.println();

Arrays.spliterator(array, 1, 4).
        forEachRemaining((IntConsumer) value -> System.out.print(value + ","));
System.out.println();

Arrays.spliterator(array, 2, 4).
        forEachRemaining((IntConsumer) value -> System.out.print(value + ","));
System.out.println();

Arrays.spliterator(array, 3, 4).
        forEachRemaining((IntConsumer) value -> System.out.print(value + ","));
System.out.println();

// Result
// ↓
//1,2,3,4,
//2,3,4,
//3,4,
//4,

static Spliterator.OfLong spliterator (long[] array)

Returns a Spliterator.OfLong covering all of the specified array.

This method is equivalent except a type to spliterator(int[] array).

static Spliterator.OfLong spliterator (long[] array, int startInclusive, int endExclusive)

Returns a Spliterator.OfLong covering the specified range of the specified array.

This method is equivalent except a type to spliterator(int[] array, int startInclusive, int endExclusive).

static <T> Spliterator<T> spliterator (T[] array)

Returns a Spliterator covering all of the specified array.

final String[] array = {"a", "b", "c", "d"};

final var spliterator = Arrays.spliterator(array);

spliterator.forEachRemaining(value -> {
    System.out.print(value + ",");
});
System.out.println();

// Result
// ↓
//a,b,c,d,

static <T> Spliterator<T> spliterator (T[] array, int startInclusive, int endExclusive)

Returns a Spliterator covering the specified range of the specified array.

final String[] array = {"a", "b", "c", "d"};

Arrays.spliterator(array, 0, 4).forEachRemaining(value -> System.out.print(value + ","));
System.out.println();

Arrays.spliterator(array, 0, 3).forEachRemaining(value -> System.out.print(value + ","));
System.out.println();

Arrays.spliterator(array, 0, 2).forEachRemaining(value -> System.out.print(value + ","));
System.out.println();

Arrays.spliterator(array, 0, 1).forEachRemaining(value -> System.out.print(value + ","));
System.out.println();

// Result
// ↓
//a,b,c,d,
//a,b,c,
//a,b,
//a,
final String[] array = {"a", "b", "c", "d"};

Arrays.spliterator(array, 0, 4).forEachRemaining(value -> System.out.print(value + ","));
System.out.println();

Arrays.spliterator(array, 1, 4).forEachRemaining(value -> System.out.print(value + ","));
System.out.println();

Arrays.spliterator(array, 2, 4).forEachRemaining(value -> System.out.print(value + ","));
System.out.println();

Arrays.spliterator(array, 3, 4).forEachRemaining(value -> System.out.print(value + ","));
System.out.println();

// Result
// ↓
//a,b,c,d,
//b,c,d,
//c,d,
//d,

static DoubleStream stream (double[] array)

Returns a sequential DoubleStream with the specified array as its source.

This method is equivalent except a type to stream(int[] array).

static DoubleStream stream (double[] array, int startInclusive, int endExclusive)

Returns a sequential DoubleStream with the specified range of the specified array as its source.

This method is equivalent except a type to stream(int[] array, int startInclusive, int endExclusive).

static IntStream stream (int[] array)

Returns a sequential IntStream with the specified array as its source.

final int[] array = {1, 2, 3, 4};

final var stream = Arrays.stream(array);

final var s = stream.mapToObj(value -> String.valueOf(value * 2)).collect(Collectors.joining(", "));
System.out.println(s); // 2, 4, 6, 8

static IntStream stream (int[] array, int startInclusive, int endExclusive)

Returns a sequential IntStream with the specified range of the specified array as its source.

final int[] array = {1, 2, 3, 4};

final var s1 = Arrays.stream(array, 0, 4)
        .mapToObj(value -> String.valueOf(value * 2)).collect(Collectors.joining(", "));

final var s2 = Arrays.stream(array, 0, 3)
        .mapToObj(value -> String.valueOf(value * 2)).collect(Collectors.joining(", "));

final var s3 = Arrays.stream(array, 0, 2)
        .mapToObj(value -> String.valueOf(value * 2)).collect(Collectors.joining(", "));

final var s4 = Arrays.stream(array, 0, 1)
        .mapToObj(value -> String.valueOf(value * 2)).collect(Collectors.joining(", "));

System.out.println(s1); // 2, 4, 6, 8
System.out.println(s2); // 2, 4, 6
System.out.println(s3); // 2, 4
System.out.println(s4); // 2
final int[] array = {1, 2, 3, 4};

final var s1 = Arrays.stream(array, 0, 4)
        .mapToObj(value -> String.valueOf(value * 2)).collect(Collectors.joining(", "));

final var s2 = Arrays.stream(array, 1, 4)
        .mapToObj(value -> String.valueOf(value * 2)).collect(Collectors.joining(", "));

final var s3 = Arrays.stream(array, 2, 4)
        .mapToObj(value -> String.valueOf(value * 2)).collect(Collectors.joining(", "));

final var s4 = Arrays.stream(array, 3, 4)
        .mapToObj(value -> String.valueOf(value * 2)).collect(Collectors.joining(", "));

System.out.println(s1); // 2, 4, 6, 8
System.out.println(s2); // 4, 6, 8
System.out.println(s3); // 6, 8
System.out.println(s4); // 8

static LongStream stream (long[] array)

Returns a sequential LongStream with the specified array as its source.

This method is equivalent except a type to stream(int[] array).

static LongStream stream (long[] array, int startInclusive, int endExclusive)

Returns a sequential LongStream with the specified range of the specified array as its source.

This method is equivalent except a type to stream(int[] array, int startInclusive, int endExclusive).

static <T> Stream<T> stream (T[] array)

Returns a sequential Stream with the specified array as its source.

final String[] array = {"a", "b", "c", "d"};

final var stream = Arrays.stream(array);

final var s = stream.map(value -> value.repeat(3)).collect(Collectors.joining(", "));
System.out.println(s); // aaa, bbb, ccc, ddd

static <T> Stream<T> stream (T[] array, int startInclusive, int endExclusive)

Returns a sequential Stream with the specified range of the specified array as its source.

final String[] array = {"a", "b", "c", "d"};

final var s1 = Arrays.stream(array, 0, 4)
        .map(value -> value.repeat(3)).collect(Collectors.joining(", "));

final var s2 = Arrays.stream(array, 0, 3)
        .map(value -> value.repeat(3)).collect(Collectors.joining(", "));

final var s3 = Arrays.stream(array, 0, 2)
        .map(value -> value.repeat(3)).collect(Collectors.joining(", "));

final var s4 = Arrays.stream(array, 0, 1)
        .map(value -> value.repeat(3)).collect(Collectors.joining(", "));

System.out.println(s1); // aaa, bbb, ccc, ddd
System.out.println(s2); // aaa, bbb, ccc
System.out.println(s3); // aaa, bbb
System.out.println(s4); // aaa
final String[] array = {"a", "b", "c", "d"};

final var s1 = Arrays.stream(array, 0, 4)
        .map(value -> value.repeat(3)).collect(Collectors.joining(", "));

final var s2 = Arrays.stream(array, 1, 4)
        .map(value -> value.repeat(3)).collect(Collectors.joining(", "));

final var s3 = Arrays.stream(array, 2, 4)
        .map(value -> value.repeat(3)).collect(Collectors.joining(", "));

final var s4 = Arrays.stream(array, 3, 4)
        .map(value -> value.repeat(3)).collect(Collectors.joining(", "));

System.out.println(s1); // aaa, bbb, ccc, ddd
System.out.println(s2); // bbb, ccc, ddd
System.out.println(s3); // ccc, ddd
System.out.println(s4); // ddd

static String toString (boolean[] a)

Returns a string representation of the contents of the specified array.

final boolean[] a = {true, false, true, false};
System.out.println(Arrays.toString(a)); // [true, false, true, false]

static String toString (byte[] a)

Returns a string representation of the contents of the specified array.

This method is equivalent except a type to toString(int[] a).

static String toString (char[] a)

Returns a string representation of the contents of the specified array.

final char[] a = {'a', 'b', 'c', 'd'};
System.out.println(Arrays.toString(a)); // [a, b, c, d]

static String toString (double[] a)

Returns a string representation of the contents of the specified array.

This method is equivalent except a type to toString(int[] a).

static String toString (float[] a)

Returns a string representation of the contents of the specified array.

This method is equivalent except a type to toString(int[] a).

static String toString (int[] a)

Returns a string representation of the contents of the specified array.

final int[] a = {1, 2, 3, 4};
System.out.println(Arrays.toString(a)); // [1, 2, 3, 4]

static String toString (long[] a)

Returns a string representation of the contents of the specified array.

This method is equivalent except a type to toString(int[] a).

static String toString (short[] a)

Returns a string representation of the contents of the specified array.

This method is equivalent except a type to toString(int[] a).

static String toString (Object[] a)

Returns a string representation of the contents of the specified array.

final String[] a = {"a", "b", "c", "d"};
System.out.println(Arrays.toString(a)); // [a, b, c, d]

Related posts

To top of page