//  Data Structures with Java by John R. Hubbard
//  Copyright McGraw-Hill, 2001
//  Problem 5.11 on page 107

  public Object[] toArray(Object[] objects)
  { // returns an array whose elements are in this bag
    // if it is longer than the given object[] array;
    // otherwise, the given array is returned after removing
    // all of its elements and then loading it with the elements
    // of this bag, and padding the rest of it with nulls;
    if (size > objects.length) objects = this.toArray();
    else
    { for (int i=0; i<size; i++)
        objects[i] = this.objects[i];
      for (int i=size; i<objects.length; i++)
        objects[i] = null;
    }
    return objects;
  }
