1: // Version 4.0 2: /** Retrieves all entries that are in this bag. 3: @return a newly allocated array of all the entries in the bag. */ 4: public T[] toArray() 5: { 6: // The cast is safe because the new array contains null entries 7: @SuppressWarnings("unchecked") 8: T[] result = (T[])new Object[numberOfEntries]; // Unchecked cast 9: 10: int index = 0; 11: Node currentNode = firstNode; 12: while ((index < numberOfEntries) && (currentNode != null)) 13: { 14: result[index] = currentNode.data; 15: index++; 16: currentNode = currentNode.next; 17: } // end while 18: 19: return result; 20: } // end toArray