【发布时间】:2014-04-26 16:49:44
【问题描述】:
public int indexOf(X item)
{
Node <X> current = head;
int c = 0;
if (current.getValue().equals(item))
{
return c;
}
while (current != null)
{
current = current.getLink();
c++;
if (current.getValue().equals(item))
{
return c;
}
}
return -1;
}
.
@Test
public void testIndexOf()
{
LList<String> b = new LList<String>();
for (int i = 0; i < 20; i++)
b.add("str" + i);
assertEquals(19, b.indexOf("str0"));
assertEquals(0, b.indexOf("str19"));
assertEquals(-1, b.indexOf("not found"));
}
由于某种原因,最后一个断言作为 Nullpointer 异常引发了错误。但是我做到了,所以当它确实达到 null 时返回 -1,这是我在测试的第三个断言中试图返回的,我在这里缺少什么?
【问题讨论】:
-
查看异常中的行号了吗?您对堆栈跟踪的哪一部分感到困惑(假设您这样做了,否则您不会在这里发布)?
标签: java arrays list hyperlink