java代码中绑定异常错误的数组索引

时间:2015-12-02 15:00:57

标签: java iterator

我写了一段代码,这些代码可以让我创建整数的子集。

   import java.util.Iterator;
    import java.util.NoSuchElementException;

    public class FixedRangeIntegerSet implements Iterable<Integer> {

        private boolean[] elementFlags;
        private int size;
        private int lowerBound;
        private int upperBound;

        public FixedRangeIntegerSet(int lowerBound, int upperBound) {
            this.lowerBound = lowerBound;
            this.upperBound = upperBound;
            int size = upperBound - lowerBound + 1;
            elementFlags = new boolean[size + 1];
            this.size = size;
        }

        public FixedRangeIntegerSet(int lowerBound, int upperBound, Iterable<Integer> elements) {
            int sizeCopy = size;
            for (Integer element : elements) {
                if (sizeCopy > 0) {
                    add(element);
                    sizeCopy--;
                }
            }
        }

        public FixedRangeIntegerSet(int lowerBound, int upperBound, Integer... elements) {
            for (int i = 0; i < elements.length; i++) {
                add(elements[i]);
            }
        }

        public boolean add(Integer element) {
            if (elementFlags[element - lowerBound] == true) {
                return false;
            }
            elementFlags[element - lowerBound] = true;
            return true;
        }

        public boolean containsElement(Integer element) {
            if (element < lowerBound || element > upperBound) {
                return false;
            }
            if (elementFlags[element - lowerBound] == true) {
                return true;
            } else
                return false;
        }

        public int getSize() {
            int size = 0;
            for (Boolean i : elementFlags) {
                if (i == true) {
                    size++;
                }
            }
            return size;
        }

        public int getLowerBound() {
            return lowerBound;
        }

        public int getUpperBound() {
            return upperBound;
        }

        public boolean remove(Integer element) {
            if (elementFlags[element - lowerBound] == false) {
                return false;
            } else {
                elementFlags[element - lowerBound] = false;
                return true;
            }
        }

        @Override
        public Iterator<Integer> iterator() {
            return new RangeIter();
        }

        private class RangeIter implements Iterator<Integer> {

            private int currentIndex = 0;

            @Override
            public boolean hasNext() {
                return currentIndex < size - 1; 
currentIndex < size


            }

            @Override
            public Integer next() {
                if (!hasNext()) {
                    throw new NoSuchElementException("No elements more!");
                }
                Integer element = currentIndex;
                while (elementFlags[currentIndex] == false && currentIndex <=size) {
                    currentIndex++;
                }
                return currentIndex++ + lowerBound;
            }
        }
    }

我在这一行得到了数组索引输出异常:

while (elementFlags[currentIndex] == false && currentIndex <= size) {

我认为问题出在Iterator上。 你能帮我解决这段代码吗? 非常感谢!

1 个答案:

答案 0 :(得分:2)

你应该这样做:

while (currentIndex <= size && elementFlags[currentIndex] == false) {

您想先检查尺码。如果currentIndex不是<= size,那么它甚至不会尝试elementFlags[currentIndex]