如何为这个多集解决这个超出范围的异常?

时间:2017-09-23 02:49:25

标签: java arrays collections multiset

我不断遇到越界异常。我检查了其他来源,这似乎主要是由于一个错误的循环,但我的很好。

数组_store和arr都有条目。

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();
  }

中间问题:

 /**
   * 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

1 个答案:

答案 0 :(得分:0)

看起来您正在使用Eclipse IDE。打开视图“断点”。点击“J!”图标在对话框中键入异常的类名,然后选择它。这会创建一个断点,在将要抛出异常时停止应用程序。程序停止后,您可以在“变量”视图中看到各种变量信息。

某些区域允许您可以单击异常的快捷方式,Eclipse将弹出特定于该异常的对话框。应该有一个选项来打开异常的调试断点。