0
試圖實現一個SET接口並使用Iterator作爲聯合方法。在union方法中,它永遠不會進入while循環。它不會添加聯合方法所具有的參數「set」中的元素。 任何想法? :)set.iterator().......我在哪裏錯了?
package linkedSet;
import java.util.Iterator; import java.util.NoSuchElementException;
import linkedList.LinearNode;
公共類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;
}
}
調試器說什麼?你現在怎麼稱呼這種方法?我們看不到你的主要方法,所以它可能是空集 –
歡迎來到Stack Overflow!它看起來像你需要學習使用調試器。請幫助一些[互補調試技術](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之後仍然有問題,請隨時返回更多詳情。 –