我有一个自己创建的链接列表。我想在以下方法中检索以给定输入的最后一个字符开头的所有元素。
public String tracker(String w)
{
SampleNode d = head();
while(d != null)
{
if(w.charAt(w.length() -1) == d.getElement().charAt(0))
{
return d.getElement();
}
d = d.getNext();
}
return null;
}
我上面的代码仅返回第一个元素,但我需要所有元素。我该怎么做?谢谢!
编辑:对于任何需要正确答案的人
public List tracker(String w)
{
SampleNode d = head();
LinkedList another = new LinkedList();
while(d != null)
{
if(w.charAt(w.length() -1) == d.getElement().charAt(0))
{
another.add(d.getElement());
}
d = d.getNext();
}
return another;
}
我不会给你工作代码,但是可以告诉你怎么做。
您只是在循环内立即返回该元素。那就是你想要的。
获取列表,然后继续添加,最后返回列表。
您差不多完成了,只是获取一个新列表,如果条件添加到列表中,然后最后返回。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句