Source of 3.14.java


  1:         /** Retrieves all entries that are in this bag.
  2:             @return  A newly allocated array of all the entries in the bag.
  3:        @author Frank M. Carrano, Timothy M. Henry
  4:        @version 5.0
  5:    */
  6:         public T[] toArray()
  7:         {
  8:       // The cast is safe because the new array contains null entries
  9:       @SuppressWarnings("unchecked")
 10:       T[] result = (T[])new Object[numberOfEntries]; // Unchecked cast

 12:       int index = 0;
 13:       Node currentNode = firstNode;
 14:       while ((index < numberOfEntries) && (currentNode != null))
 15:       {
 16:          result[index] = currentNode.data;
 17:          index++;
 18:          currentNode = currentNode.next;
 19:       } // end while
 20:               
 21:                 return result;
 22:         } // end toArray