【发布时间】:2017-12-08 09:50:19
【问题描述】:
为了准备我的考试,我正在写一些给我们的旧考试。在其中一个中,我们必须实现一个 EnterpriseNode。一项任务是编写一个遍历 Staffmembers 的迭代器。我写了函数 next() 但它不能正常工作。 这是我的代码:
public StaffMemberIterator(HashSet<StaffMember> directSubordinates){
if(directSubordinates == null) throw new NullPointerException("ofs");
this.directSubordinates = directSubordinates;
}
public StaffMember next() {
if (!this.hasNext()){
throw new NoSuchElementException("naf");
}
ArrayList<StaffMember> subList = new ArrayList<>(directSubordinates);
StaffMember staffMember = subList.get(position);
position++;
return staffMember;
}
这是错误:
junit.framework.AssertionFailedError: StaffMemberIterator.next() should return the correct next element if there is one available! expected:<Name: Allie, Job: J1> but was:<Name: Lilith, Job: J3>
at StaffMemberIteratorTest.testIterator(StaffMemberIteratorTest.java:48)
谢谢大家的帮助!
【问题讨论】:
-
所以您有 JUnit 测试,但没有尽可能多地利用它们?您是否尝试过调试以查看您的迭代器做了什么? hasNext() 是如何工作的?有很多问题,但需要处理的代码并不多。
-
我没有看到任何对集合成员施加特定顺序的东西。
-
欢迎来到 Stack Overflow!看来您需要学习使用调试器。请帮助自己一些complementary debugging techniques。如果您之后仍有问题,请随时返回 Minimal, Complete and Verifiable Example 来证明您的问题。