Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么不是';我的迭代器不工作吗?_Java_Iterator - Fatal编程技术网

Java 为什么不是';我的迭代器不工作吗?

Java 为什么不是';我的迭代器不工作吗?,java,iterator,Java,Iterator,对于类赋值,我必须从头实现自己的迭代器。迭代器正在遍历节点的链接列表。我所有使用迭代器的测试用例都失败了,我不知道它出了什么问题 import java.util.Iterator; import java.util.NoSuchElementException; class LinkedNodeIterator<E> implements Iterator<E> { LinkedNode<E> headNode; LinkedNode<

对于类赋值,我必须从头实现自己的迭代器。迭代器正在遍历节点的链接列表。我所有使用迭代器的测试用例都失败了,我不知道它出了什么问题

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

class LinkedNodeIterator<E> implements Iterator<E> {
    LinkedNode<E> headNode;
    LinkedNode<E> curr;


  // Constructors
  public LinkedNodeIterator(LinkedNode<E> head) {
      headNode = head;
      curr = headNode;      
  }

  @Override
  public boolean hasNext() {
      if(headNode == null)
          return false;
      if(curr.getNext() == null)
          return false;
      return true;
  }

  @Override
  public E next() {
    if(curr.getNext() == null || curr == null)
    throw new NoSuchElementException();
        LinkedNode<E> save = curr;
        curr = curr.getNext();
        return save.getData();

  }


  @Override
  public void remove() {
    throw new UnsupportedOperationException();
  }
}
import java.util.Iterator;
导入java.util.NoSuchElementException;
类LinkedNodeIterator实现迭代器{
链接节点头节点;
连接节点电流;
//建设者
公共LinkedNode迭代器(LinkedNode头){
头节点=头;
curr=头节点;
}
@凌驾
公共布尔hasNext(){
if(headNode==null)
返回false;
if(curr.getNext()==null)
返回false;
返回true;
}
@凌驾
公共教育{
if(curr.getNext()==null | | curr==null)
抛出新的NoTouchElementException();
LinkedNode save=curr;
curr=curr.getNext();
返回save.getData();
}
@凌驾
公共空间删除(){
抛出新的UnsupportedOperationException();
}
}
一些失败的测试用例(它们都返回count=0):

公共类PublicLinkedSetTest{
设置为set0;
设置set1;
设置set2;
设置set3;
设置set4;
@以前
在()之前公开无效{
set0=新链接集();
set1=新链接集();
for(字符串e:新字符串[]{“c”、“a”、“d”、“b”、“e”}){
set1=set1.邻接(e);
}
set2=新链接集();
for(字符串e:新字符串[]{“b”、“d”、“a”、“e”、“c”}){
set2=set2.邻接(e);
}
set3=新链接集();
对于(字符串e:新字符串[]{“a”、“d”、“b”}){
set3=set3.邻接(e);
}
set4=新链接集();
对于(字符串e:新字符串[]{“x”、“y”、“z”、“a”、“b”、“d”}){
set4=set4.邻接(e);
}
}
公开无效证明人1(){
整数计数=0;
for(字符串e:set1){
计数+=1;
}
资产质量(5,计数);
}
@试验
公开无效证明人2(){
整数计数=0;
用于(字符串e:set2){
计数+=1;
}
资产质量(5,计数);
}
@试验
公开无效证明人3(){
整数计数=0;
用于(字符串e:set3){
计数++;
}
资产质量(3,计数);
}
这是我的LinkedSet的代码

import java.util.Iterator;

public class LinkedSet<E> implements Set<E> {
  private LinkedNode<E> head = null;
  private LinkedNode<E> link;

  // Constructors
  public LinkedSet() {
  }

  public LinkedSet(E e) {
    this.head = new LinkedNode<E>(e, null);
  }

  private LinkedSet(LinkedNode<E> header) {
    header = head;

  }

  @Override
  public int size() {
      int count = 0;
     for(E e : this){
        count++;}
    return count;
  }

  @Override
  public boolean isEmpty() {
    for(E e : this){
        if(e != null)
            return false;
    }
        return true;
  }

  @Override
  public LinkedNodeIterator<E> iterator() {
    return new LinkedNodeIterator<E>(this.head);
  }

  @Override
  public boolean contains(Object o) {
  for(E e : this){
      if(e == o)
          return true;
  }
    return false;
  }

  @Override
  public boolean isSubset(Set<E> that) {
      that = new LinkedSet<E>();
      if(this.size()>that.size())
          return false;
    for(E e : this){
        if(that.contains(e) == false)
            return false;
    }
    return true;
  }

  @Override
  public boolean isSuperset(Set<E> that) {
   that = new LinkedSet<E>();
   if(this.isSubset(that))
    return true;
   else
       return false;
  }

  @Override
  public Set<E> adjoin(E e) {
      boolean alwaysEqual = true;
      if(this.head == null)
          return this;
    for(E t : this){
        if(t != e)
            alwaysEqual = false;}
    if(alwaysEqual == true)
        return this;
    LinkedNode<E> temp = this.head;
   LinkedNode<E> newNode = new LinkedNode<E>(e, temp);
   LinkedSet<E> newSet = new LinkedSet<E>(newNode);
   Set<E> otherSet = newSet;

    return otherSet;
  }

  @Override
  public Set<E> union(Set<E> that) {
      Set<E> thisSet = this;
    for(E e : that){
        if(!this.contains(e))
            thisSet = thisSet.adjoin(e);

    }

    return thisSet;
  }

  @Override
  public Set<E> intersect(Set<E> that) {
      LinkedSet<E> newSet = null;
      Set<E> otherNewSet = newSet;
    for(E e : that){
        if(this.contains(e)){
            if(otherNewSet == null){
                LinkedNode<E> newNode = new LinkedNode<E>(e, null);
                otherNewSet = new LinkedSet<E>(newNode);
            }
            else{

                otherNewSet = otherNewSet.adjoin(e);
            }

        }
    }

    return otherNewSet;
  }

  @Override
  public Set<E> subtract(Set<E> that) {
    LinkedSet<E> newSet = null;
    Set<E> otherNewSet = newSet;
    for(E e : that){
        if(!this.contains(e)){
            if(otherNewSet == null){
                LinkedNode<E> newNode = new LinkedNode<E>(e, null);
                otherNewSet = new LinkedSet<E>(newNode);
            }
            else{

                otherNewSet = otherNewSet.adjoin(e);
            }

        }
    }

    return otherNewSet;
        }

  @Override
  public Set<E> remove(E e) {
      LinkedSet<E> newSet = null;
        Set<E> otherNewSet = newSet;
    if(!this.contains(e))
    return this;
    else{
        for(E t : this){
            if(t != e){
                if(otherNewSet == null){
                    LinkedNode<E> newNode = new LinkedNode<E>(e, null);
                    otherNewSet = new LinkedSet<E>(newNode);
                }
            }
                else{

                    otherNewSet = otherNewSet.adjoin(e);
                }
        }
    }
    return otherNewSet;
  }

  @Override
  @SuppressWarnings("unchecked")
  public boolean equals(Object o) {
    if (! (o instanceof Set)) {
      return false;
    }
    Set<E> that = (Set<E>)o;
    return this.isSubset(that) && that.isSubset(this);
  }

  @Override
    public int hashCode() {
    int result = 0;
    for (E e : this) {
      result += e.hashCode();
    }
    return result;
  }
}
import java.util.Iterator;
公共类LinkedSet实现了{
私有LinkedNode头=null;
私有链接节点链接;
//建设者
公共链接集(){
}
公共链接集(E){
this.head=新的LinkedNode(e,null);
}
专用LinkedSet(LinkedNode头){
头=头;
}
@凌驾
公共整数大小(){
整数计数=0;
对于(E:本){
计数+++;}
返回计数;
}
@凌驾
公共布尔值为空(){
对于(E:本){
如果(e!=null)
返回false;
}
返回true;
}
@凌驾
公共LinkedNodeIterator迭代器(){
返回新的LinkedNodeIterator(this.head);
}
@凌驾
公共布尔包含(对象o){
对于(E:本){
如果(e==o)
返回true;
}
返回false;
}
@凌驾
公共布尔值IsubSet(设置该值){
that=新链接集();
如果(this.size()>that.size())
返回false;
对于(E:本){
如果(包含(e)=false)
返回false;
}
返回true;
}
@凌驾
公共布尔值isSuperset(设置该值){
that=新链接集();
如果(本发行集)
返回true;
其他的
返回false;
}
@凌驾
公共集邻接(E){
布尔值alwaysEqual=true;
if(this.head==null)
归还这个;
对于(E t:本){
如果(t!=e)
alwaysEqual=false;}
如果(alwaysEqual==真)
归还这个;
LinkedNode温度=this.head;
LinkedNode newNode=新LinkedNode(e,temp);
LinkedSet newSet=新的LinkedSet(newNode);
Set otherSet=newSet;
返回其他集合;
}
@凌驾
公共集合并集(集合该集合){
Set thisSet=this;
对于(E:那个){
如果(!this.contains(e))
此集合=此集合。邻接(e);
}
返回此集合;
}
@凌驾
公共集合相交(集合该集合){
LinkedSet newSet=null;
设置otherNewSet=newSet;
对于(E:那个){
如果(本节包含(e)){
if(otherNewSet==null){
LinkedNode newNode=新LinkedNode(e,null);
otherNewSet=新链接集(newNode);
}
否则{
otherNewSet=otherNewSet.邻接(e);
}
}
}
返回其他新闻集;
}
@凌驾
公共集减法(设置该值){
LinkedSet newSet=null;
设置otherNewSet=newSet;
对于(E:那个){
如果(!this.contains(e)){
if(otherNewSet==null){
LinkedNode newNode=新LinkedNode(e,null);
otherNewSet=新链接集(newNode);
}
否则{
otherNewSet=otherNewSet.邻接(e);
}
}
}
返回其他新闻集;
}
@凌驾
公共集删除(E){
LinkedSet newSet=null;
设置otherNewSet=newSet;
如果(!this.contains(e))
归还这个;
否则{
对于(E t:本){
如果(t!=e){
if(otherNewSet==null){
LinkedNode newNode=新LinkedNode(e,null);
otherNewSet=新链接集(newNode);
}
}
否则{
otherNewSet=otherNewSet.邻接(e);
}
}
}
返回其他新闻集;
}
@凌驾
@抑制警告(“未选中”)
公共布尔等于(对象o){
如果(!(集合的o实例)){
返回false;
}
设置为=(设置)o;
返回此.isSubset(that)和&that.isSubset(this);
}
@凌驾
公共int hashCode(){
int结果=0;
对于(E:本){
结果+=e.hashCode();
}
返回结果;
}
}

问题不在于迭代器,而在于你的
邻接方法

当你构造一个
import java.util.Iterator;

public class LinkedSet<E> implements Set<E> {
  private LinkedNode<E> head = null;
  private LinkedNode<E> link;

  // Constructors
  public LinkedSet() {
  }

  public LinkedSet(E e) {
    this.head = new LinkedNode<E>(e, null);
  }

  private LinkedSet(LinkedNode<E> header) {
    header = head;

  }

  @Override
  public int size() {
      int count = 0;
     for(E e : this){
        count++;}
    return count;
  }

  @Override
  public boolean isEmpty() {
    for(E e : this){
        if(e != null)
            return false;
    }
        return true;
  }

  @Override
  public LinkedNodeIterator<E> iterator() {
    return new LinkedNodeIterator<E>(this.head);
  }

  @Override
  public boolean contains(Object o) {
  for(E e : this){
      if(e == o)
          return true;
  }
    return false;
  }

  @Override
  public boolean isSubset(Set<E> that) {
      that = new LinkedSet<E>();
      if(this.size()>that.size())
          return false;
    for(E e : this){
        if(that.contains(e) == false)
            return false;
    }
    return true;
  }

  @Override
  public boolean isSuperset(Set<E> that) {
   that = new LinkedSet<E>();
   if(this.isSubset(that))
    return true;
   else
       return false;
  }

  @Override
  public Set<E> adjoin(E e) {
      boolean alwaysEqual = true;
      if(this.head == null)
          return this;
    for(E t : this){
        if(t != e)
            alwaysEqual = false;}
    if(alwaysEqual == true)
        return this;
    LinkedNode<E> temp = this.head;
   LinkedNode<E> newNode = new LinkedNode<E>(e, temp);
   LinkedSet<E> newSet = new LinkedSet<E>(newNode);
   Set<E> otherSet = newSet;

    return otherSet;
  }

  @Override
  public Set<E> union(Set<E> that) {
      Set<E> thisSet = this;
    for(E e : that){
        if(!this.contains(e))
            thisSet = thisSet.adjoin(e);

    }

    return thisSet;
  }

  @Override
  public Set<E> intersect(Set<E> that) {
      LinkedSet<E> newSet = null;
      Set<E> otherNewSet = newSet;
    for(E e : that){
        if(this.contains(e)){
            if(otherNewSet == null){
                LinkedNode<E> newNode = new LinkedNode<E>(e, null);
                otherNewSet = new LinkedSet<E>(newNode);
            }
            else{

                otherNewSet = otherNewSet.adjoin(e);
            }

        }
    }

    return otherNewSet;
  }

  @Override
  public Set<E> subtract(Set<E> that) {
    LinkedSet<E> newSet = null;
    Set<E> otherNewSet = newSet;
    for(E e : that){
        if(!this.contains(e)){
            if(otherNewSet == null){
                LinkedNode<E> newNode = new LinkedNode<E>(e, null);
                otherNewSet = new LinkedSet<E>(newNode);
            }
            else{

                otherNewSet = otherNewSet.adjoin(e);
            }

        }
    }

    return otherNewSet;
        }

  @Override
  public Set<E> remove(E e) {
      LinkedSet<E> newSet = null;
        Set<E> otherNewSet = newSet;
    if(!this.contains(e))
    return this;
    else{
        for(E t : this){
            if(t != e){
                if(otherNewSet == null){
                    LinkedNode<E> newNode = new LinkedNode<E>(e, null);
                    otherNewSet = new LinkedSet<E>(newNode);
                }
            }
                else{

                    otherNewSet = otherNewSet.adjoin(e);
                }
        }
    }
    return otherNewSet;
  }

  @Override
  @SuppressWarnings("unchecked")
  public boolean equals(Object o) {
    if (! (o instanceof Set)) {
      return false;
    }
    Set<E> that = (Set<E>)o;
    return this.isSubset(that) && that.isSubset(this);
  }

  @Override
    public int hashCode() {
    int result = 0;
    for (E e : this) {
      result += e.hashCode();
    }
    return result;
  }
}
 set1 = new LinkedSet<String>();
    // Constructors
    public LinkedSet() {
    }
    private LinkedNode<E> head = null;
@Override
public Set<E> adjoin(E e) {
    boolean alwaysEqual = true;
    if (this.head == null)
        return this;
    for (E t : this) {
        if (t != e)
            alwaysEqual = false;
    }
    if (alwaysEqual == true)
        return this;
    LinkedNode<E> temp = this.head;
    LinkedNode<E> newNode = new LinkedNode<E>(e, temp);
    LinkedSet<E> newSet = new LinkedSet<E>(newNode);
    Set<E> otherSet = newSet;

    return otherSet;
}