我想将字符串存储在LinkedList中。我不允许预先排序,但找到该位置并将该字符串传递给链接列表。当我通过文本文件传递字符串时,字符串不会通过最后一个条件。 我的输入文件有 乔 APPY appz 斑马 猫找到正确的地方并存储在LinkedList中
当它到达appz,它不通过任何声明去。它应该插入最后一个条件并打印5,但不这样做。
/**
* Gets the string and arranges them in order
* @param newString
*/
public void store(String newString) {
LinkedListNode current = head;
System.out.println(newString);
// if no element in the list
if (current==null){
System.out.println("1");
makeNode(newString);
}
// if only 1 elements in the list
else if(current.getNext()==null){
System.out.println("2");
if(newString.compareTo(current.getName())<0){
insertBefore(current.getName(),newString);
} else{
insertAfter(current.getName(),newString);
}
}
// if the element is smaller than the head in the list
else if(newString.compareTo(current.getName()) < 0){
System.out.println("3");
LinkedListNode temp = makeNode(newString);
temp.setNext(current);
head=temp;
}
// if the element is greater than the tail in the list
else if(newString.compareTo(findTail().getName()) > 0){
System.out.println("4");
insertAfter(findTail().getName(),newString);
}
// for more than two elements in the list
else{
System.out.println("5");
while(!(newString.compareTo(current.getName())>0 && newString.compareTo(current.getNext().getName())<0) && current.getNext()!=null){
current=current.getNext();
}
if(newString.compareTo(current.getName())<0){
insertBefore(current.getName(),newString);
}
else{
insertAfter(current.getName(),newString);
}
}
} // end of store()
只有appz有这个问题,或者它是从appz和以后的一切吗?如果没有前面的条件执行,它看起来应该执行最后的其他条件。所以无论如何你看它,它应该打印出一个数字 - 对吗? – ucsunil 2014-10-17 18:41:48
@Sunil:亚苏尔,我应该至少去上一条语句,但是在打印5之前停下来,然而程序继续运行。在appz的位置冻结。 – Sooner 2014-10-17 18:43:52
嗯。有趣。由于我没有全部代码,因此我需要你在第一个if(if(current == null)行)到最后一个else(打印5行上面的那一行)的每一行中放置断点。调试程序,你可以让我知道程序停在哪一行?如果一切都是正确的,它应该一路走到最后,但显然这没有发生。让我知道哪一行,或者你可以将所有的代码转储到哪里,我可以看看它 – ucsunil 2014-10-17 18:56:50