set.iterator().......我哪里错了?

时间:2016-11-05 21:27:20

标签: java collections iterator set

尝试实现SET接口并将Iterator用于union方法。在union方法中,它永远不会进入while循环。它不会添加union方法所具有的参数“set”中的元素。 有任何想法吗? :)

package linkedSet;

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

import linkedList.LinearNode;

public class LinkedSet实现了SetADT {

private int size;
private LinearNode<T> front;
private boolean allowNullElement = false;

// /////////////////////////////////////////////////////
private class LinkedSetIterator implements Iterator<T> {

    private LinearNode<T> currentNode;

    public LinkedSetIterator() {
        currentNode = front;
    }

    @Override
    public boolean hasNext() {
        return currentNode == null;
    }

    @Override
    public T next() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        T temp = currentNode.getElement();
        currentNode = currentNode.getNext();
        return temp;
    }
}
///////////////////////////////////////////////////
@Override
public Iterator<T> iterator() {
    return new LinkedSetIterator();

}
public LinkedSet(boolean allowNullElement) {
    size = 0;
    front = new LinearNode<T>();
    this.allowNullElement = allowNullElement;
}

public String toString() {

    String str = "{";
    for (int i = 0; i < size; i++) {
        str += getNode(i).getElement();
        if (i < size - 1) {
            str += (", ");
        }
    }
    return str + "}";

}

private LinearNode<T> getNode(int index) {
    LinearNode<T> current = front;
    for (int i = 0; i < index; i++) {
        current = current.getNext();
    }
    return current;
}

@Override
public void add(T element) {
    if (element == null && !allowNullElement) {
        throw new IllegalArgumentException(
                "Null element is not allowed to add");
    }
    front = new LinearNode<T>(element, front);
    size++;

}

@Override
public T remove(T element) {

    if (!(contains(element))) {
        throw new IllegalStateException();
    }
    T temp = null;
    ;
    for (int i = 0; i < size; i++) {

        temp = getNode(i).getElement();
        if (temp.equals(element) || temp == element) {
            if (i == 0) {
                front = front.getNext();
                break;

            } else {
                getNode(i).setElement(front.getElement());
                front = front.getNext();
                break;
            }
        }
    }
    size--;
    return temp;
}

@Override
public boolean contains(T element) {
    for (int i = 0; i < size; i++) {
        if (getNode(i).getElement() != null
                && getNode(i).getElement().equals(element)) {
            return true;
        }
        if (getNode(i).getElement() == null
                && getNode(i).getElement() == element) {
            return true;
        }
    }
    return false;
}

@Override
public boolean isEmpty() {

    return size == 0;
}

@Override
public int size() {
    return size;
}

@Override
public boolean isSubset(SetADT<T> set) {
    if (set == null) {
        throw new IllegalArgumentException();
    }

    for (int i = 0; i < size; i++) {
        T temp = getNode(i).getElement();
        if (!(set.contains(temp))) {
            return false;
        }
    }
    return true;
}

@Override
public SetADT<T> intersection(SetADT<T> set) {
    if (set == null) {
        throw new IllegalArgumentException();
    }
    LinkedSet<T> temp = new LinkedSet<T>(allowNullElement);

    for (int i = 0; i < size; i++) {

        if (set.contains(getNode(i).getElement())) {
            temp.add(getNode(i).getElement());
        }
    }
    return temp;
}

public SetADT<T> union(SetADT<T> set) {

    if (set == null) {
        throw new IllegalArgumentException();
    }

    LinkedSet<T> temp = new LinkedSet<T>(allowNullElement);
    LinearNode<T> node = front;

    for (int i = 0; i < size; i++) {
        T el = node.getElement();
        temp.add(el);
        node = node.getNext();
    }

    Iterator<T> iterator = set.iterator();
    while (iterator.hasNext()) {
        temp.add(iterator.next());

    }
    return temp;
}

}

1 个答案:

答案 0 :(得分:3)

如果集合非空,LinkedSetIterator.hasNext()方法将返回false。这可以通过反转条件来纠正:

@Override
public boolean hasNext() {
    return currentNode != null;
}