Java Reference
In-Depth Information
Array.set(xa, i, 23);
If the object passed as the array is not actually an array, you will get an
IllegalArgumentException . If the value to be set is not assignable to the
component type of the array (after unwrapping, if necessary), you will
get an IllegalArgumentException .
The Array class also supports a full set of get Type and set Type methods
for all the primitive types, as in
Array.setInt(xa, i, 23);
These avoid the need for intermediate wrapper objects.
16.10.1. Genericity and Dynamic Arrays
Recall the toArray method of the SingleLinkQueue class from Chapter 11 .
We promised back then that we'd show you how to create the array dir-
ectly (instead of having it passed in) by having the type token for the
actual type argument of the queue passed in. Here's a first attempt:
public E[] toArray_v1(Class<E> type) {
int size = size();
E[] arr = (E[]) Array.newInstance(type, size);
int i = 0;
for (Cell<E> c = head;
c != null && i < size;
c = c.getNext())
arr[i++] = c.getElement();
return arr;
}
 
Search WWH ::




Custom Search