Arrays Class

Package: java.util

The Arrays class provides a collection of static methods that are useful for working with arrays.

For more information about arrays, see Arrays in Part 2.

Because the Arrays class provides only static methods, it has no constructors.

Methods

Method

Description

static int binarySearch ( array, key)

Searches for the specified key value in an array. The return value is the index of the element that matches the key. The method returns -1 if the key can’t be found. The array and the key must be of the same type and can be any primitive type or an object.

static array copyOf (arrayOriginal, newLength)

Returns an array that’s a copy of arrayOriginal. The newLength parameter need not equal the original array’s length. If newLength is larger, the method pads the new array with zeros. If newLength is smaller, the method doesn’t copy all of the original array’s values.

static array copyOfRange (arrayOriginal, from, to)

Does what the copyOf method does, but copies only a selected slice of values (from one index to another) of the original array.

boolean deepEquals( array1, array2)

Returns true if the two arrays have the same element values. This method works for arrays of two or more dimensions.

boolean equals( array1, array2)

Returns true if the two arrays have the same element values. This method checks equality only for one-dimensional arrays.

static void fill( array, value)

Fills the array with the specified ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.