迭代现有数组时出现空指针异常 - 写入构造函数错误

时间:2015-12-03 13:22:18

标签: java arrays constructor

类的主要思想是实现Integer子阵列。例如,我想将数字放在10到20之间的数组中。如果number在数组中,则数组中将存在true inits。数字10在数组中有索引0

我的第二个构造函数出现了问题:

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

以下是我的其余代码:

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];
        this.size = size;
    }

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

    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;

        public RangeIter() {
            this.currentIndex = 0;
        }

        @Override
        public boolean hasNext() {
            for (int i = currentIndex; i < elementFlags.length; i++) {
                if (elementFlags[i] == true)
                    return true;
            }
            return false;
        }

        @Override
        public Integer next() {

            if (!hasNext()) {
                throw new NoSuchElementException("No elements");
            }
            while (currentIndex < size - 1 && !elementFlags[currentIndex]) {
                currentIndex++;
            }
            return currentIndex++ + lowerBound;
        }
    }
}

这是main(测试)方法的伪代码:

 FixedRangeIntegerSet s1 = new FixedRangeIntegerSet(10, 20);
            s1.add(14);
            s1.add(16);
            s1.add(18);
            System.out.println("Elements:");
    foreach loop;
FixedRangeIntegerSet s2 = new FixedRangeIntegerSet(10, 20, s1);
        s2.remove(16); // here is where I get error

毕竟,我的问题是: 为什么我得到空指针异常,或者在我的代码中编写第二个构造函数我错了?谢谢!

1 个答案:

答案 0 :(得分:2)

elementFlags 未在第二个构造函数中初始化。在add方法中,您正在访问它。但它是null,所以你得到了NullPointerException