-2

I keep getting an out-of-bounds-exception. I've checked other sources and it mostly seems to be due to a faulty loops, but mine is fine.

Both arrays _store and arr have entries.

HEADER CODE:

public class ArrayMultiSet<E> implements Collection<E> {
  /** Unless otherwise specified, the default length to which the backing store should be initialized. */
  private static final int DEFAULT_INITIAL_CAPACITY = 16;

  /** Array in which the elements in this multiset are stored. */
  private E[] _store;
  /**
   * Array indices below this amount contain the active elements in this collection.
   */
  private int _size;

  /**
   * Create a new empty multiset.
   */
  public ArrayMultiSet() {
    clear();
  }

MIDDLE QUESTION:

 /**
   * Resets the Multiset so that it only contains the entries in the given array. This overwrites the data previously in
   * the Collection.
   *
   * @param arr The array whose entries will be the elements in the Collection
   */
  @SuppressWarnings("unchecked")
  public void fromArray(E[] arr) {
      // IMPORTANT: You CANNOT set the backing store to be equal to ("alias")
      // arr. If you did this, the calling method could make changes to the
      // Multiset by updating the entries in arr rather than using the Multiset methods.
      // This violates good OO practice and creates the potential for bugs and hacks.

      _size = arr.length;

      int i = 0;

      while (i < _size) {
          _store[i] = arr[i];
          i++;

      }
  }

JUNIT:

enter image description here

Gintoki
  • 97
  • 1
  • 9

1 Answers1

0

It looks like you are using Eclipse IDE. Open the view "Breakpoints". Click on the icon that is a 'J!' Type in the class name of your exception in the dialog and select it. This creates a breakpoint that will stop the application when the exception is about to be thrown. Once your program is stopped, you can see various variable information in the view "Variables".

Some areas allow shortcuts where you can click on an exception and Eclipse will popup a dialog specific to that exception. There should be an option to turn on debug breakpoints for the exception there, too.

ProgrammersBlock
  • 5,974
  • 4
  • 17
  • 21