以下是ArrayList<E>
类中一段源码,众所周知此类是泛型类,但是会出现泛型擦除,问题就是return (E) elementData[index]
中这个(E)
岂不是无用了?
transient Object[] elementData; // non-private to simplify nested class access
// Positional Access Operations
@SuppressWarnings("unchecked")
E elementData(int index) {
return (E) elementData[index];
}
/**
* Returns the element at the specified position in this list.
*
* @param index index of the element to return
* @return the element at the specified position in this list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
有用的,因为前面
elementData
声明为Object[]
了。