implementation would use a technique that is not worse than O(nlogn)
binarySearch Searches a sorted array for a given key. Returns the key's index, or a negative value
encoding a safe insertion point (as for the method Collections.binarySearch described
previously). There are no subarray versions of these methods.
•
- fill Fills in the array with a specified value.
equals and deepEquals Return true if the two arrays they are passed are the same object, are
both null, or have the same size and equivalent contents. There are no subarray versions. The
equals method for Object[] uses Object.equals on each non-null element of the array;
null elements in the first array must be matched by null elements of the second. This does not
treat nested arrays specially, so it cannot generally be used to compare arrays of arrays. The
deepEquals method checks for equivalance of two Object[] recursively taking into account the
equivalence of nested arrays.
•
hashCode and deepHashCode Return a hash code based on the contents of the given array. There
are no subarray versions. The deepHashCode method computes a hash code for an Object[]
recursively taking into account the contents of nested arrays.
•
toString and deepToString Return a string representation of the contents of the array. There
are no subarray versions. The string consists of a comma seperated list of the array's contents,
enclosed by '[' and ']'. The array contents are converted to strings with String.valueof. The
toString method for Object[] converts any nested arrays to strings using
Object.toString. The deepToString method returns a string representation of an
Object[] recursively converting nested arrays to strings as defined by this method.
•
The methods sort and binarySearch have two overloads for Object arrays. One assumes that its
elements are comparable (implement the Comparable interface). The second uses a provided
Comparator object instead, so you can manipulate arrays of objects that are not comparable or for which
you do not want to use the natural ordering.
You can view an array of objects as a List by using the object returned by the asList method.
public static
This can take an actual array reference, or it can conveniently create an array from the given sequence of
elements. The list is backed by the underlying array, so changes made to the array are visible to the list and
vice versa. The returned list allows you to set elements, but not to add or remove themit is a modifiable
list, but not a resizable list. Using a List for access to an underlying array can give you useful features of
List, such as using synchronized wrappers to synchronize access to the underlying array.
21.13. Writing Iterator Implementations
Iterators are generally useful, and you may want to write your own, even if you are not implementing a
collection type. The following code demonstrates the basics of writing your own Iterator implementation,
in this case for an iterator that will filter out strings longer than a given length:
public class ShortStrings implements Iterator
private Iterator
private String nextShort; // null if next not known
private final int maxLen; // only return strings <=
public ShortStrings(Iterator
int maxLen) {
this.strings = strings;
this.maxLen = maxLen;
nextShort = null;
}