I was using the LinkedList class in java as a guide for writing my own doubly linked list (this is for school). In this toArray method:
public <T> T[] toArray(T[] a) {
if (a.length < size)
a = (T[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
int i = 0;
Object[] result = a;
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;
if (a.length > size)
a[size] = null;
return a;
}
It seems to me that it is returning the wrong variable. I assumed it would return result
but instead it returns a
. Is there something I'm missing where filling result
with the items also affects the values in a
or am I correct that it is result
that should be returned?