public T[] toArray(){
@SuppressWarnings("unchecked")
T[] result = (T[])new Object[length]; // unchecked cast
int index = 0;
Node currentNode = firstNode;
while ((index < length) && (currentNode != null)) {
result[index] = currentNode.data;
index++;
currentNode = currentNode.next;
} // end while
return result;
} // end toArray()
public <T>T[] toArray(T[] a){
@SuppressWarnings("unchecked")
T[] result = (T[])a[length]; // unchecked cast
int index = 0;
Node currentNode = firstNode;
while ((index < length) && (currentNode != null)) {
result[index] = (T) currentNode.data;
index++;
currentNode = currentNode.next;
} // end while
return result;
} //end toArray(a)
My T[] toArray() works but I don't really understand the <T>T[] toArray(T[] a) implementation...
If i pass:
Object[] array = set4.toArray(new BigInteger[0]);
for(int k=0; k<array.length; k++){
System.out.println("at index " + k + " found " + array[k]);
I get an out of bounds exception :|Thanks

New Topic/Question
Reply



MultiQuote





|