简体   繁体   English

SortedArrayList中的Java.lang.AssertionError

[英]Java.lang.AssertionError in a SortedArrayList

So I'm making an Contact Manager App in Android. 因此,我正在使用Android制作联系人管理器应用。 My professor send us a JUnit test so we can know if our code is working correctly. 我的教授向我们发送了一个JUnit测试,以便我们知道我们的代码是否正常工作。 Now every method has passed the test except the iterator test which fails the test. 现在,除了迭代器测试失败以外,每种方法都通过了测试。 It appears to be a java.lang.AssertionError and I cant seem to figure out a way of fixing it. 它似乎是一个java.lang.AssertionError ,我似乎想不出一种解决它的方法。 If you could see the error and point it out to me would be awesome. 如果您能看到错误并向我指出错误,那就太好了。

Thanks in advance 提前致谢

You haven't implemented your iterator(int) method 您尚未实现iterator(int)方法

@Override
public Iterator<E> iterator(int index) 
{
    // TODO Auto-generated method stub
    return new ListIterator<E>();
}

to do anything about the index. 对索引做任何事情。 So in your test 所以在你的测试中

@Test
public void testIteratorInt() {
    Iterator<String> iter = this.list.iterator(0);
    assertTrue(iter.hasNext());
    assertTrue(iter.next().equals("Amy"));

    iter = this.list.iterator(2);
    assertTrue(iter.hasNext());
    assertTrue(iter.next().equals("Bob"));
}

the ListIterator returned still starts off pointing to the first element, instead of the third, which is Bob . 返回的ListIterator仍然从指向第一个元素开始,而不是指向第三个元素Bob

You could simply move the iterator forward twice and then test 您只需将迭代器向前移动两次,然后进行测试

iter.next();
iter.next();
assertTrue(iter.next().equals("Bob"));

Or actually implement the iterator(int) method as you see fit. 或者按照您认为合适的方式实现iterator(int)方法。

The error is coming off the second assert in testIteratorInt : 错误来自testIteratorInt的第二个断言:

@Test
public void testIteratorInt() {
    Iterator<String> iter = this.list.iterator(0);
    assertTrue(iter.hasNext());
    assertTrue(iter.next().equals("Amy"));

    iter = this.list.iterator(2);
    assertTrue(iter.hasNext());    
    assertTrue(iter.next().equals("Bob"));//ASSERT ERROR THROWN HERE

}

The iterator(int index) method needs to be implemented such that it sets the current position when it's passed in like the above unit test. 像上面的单元测试一样,需要实现iterator(int index)方法,以便在传入时设置当前位置。 Something like this should work: 这样的事情应该起作用:

@Override
public Iterator<E> iterator(int index) {
    ListIterator<E> iter = new ListIterator<E>();
    iter.currentPosition = index;
    return iter;
}

Tested and working on my machine. 经过测试并在我的机器上工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM