CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Arrays

Arrays
The Arrays class
Methods

The Arrays class

java.util.Arrays

The Arrays class contains many useful methods for manipulating arrays

The methods of the Arrays class

  • All of these methods accept arrays of primitive data types and arrays of objects for the arrayName argument.
  • The methods all accept primitive types and objects for the value argument.
  • All of the index arguments must be int types.
  • If an index argument is less than zero or greater than one less than the length of the array, the method will throw an ArrayIndexOutOfBoundsException.
  • If you use the sort method on an array of objects created from a user-defined class, the class must implement the Comparable interface.

Static methods of the Arrays class

Method Description
fill(arrayName, value) Fills all elements of the array with the specified value.
fill(arrayName, index1, index2, value) Fills elements of the array with the specified value from the index1 element to, but not including, the index2 element.
equals(arrayName1, arrayName2) Returns a boolean true value if both arrays are of the same type and all of the elements are equal to each other.
copyOf(arrayName, length) Copies the specified array, truncating or padding with default values as needed so the copy has the specified length.
copyOfRange(arrayName, index1, index2) Copies the specified range of the specified array into a new array.
sort(arrayName) Sorts the elements into ascending order.
sort(arrayName, index1, index2) Sorts the elements into ascending order from the index1 element to, but not including, the index2 element.
binarySearch(arrayName, value) Returns an int value for the index of the specified value in the specified array; returns a negative number if the specified value is not found. Array must first be sorted by the sort method.
Previous | Methods | Code | Comparable interface | Reference to an array | Copy | Next