简体   繁体   English

遍历所有双链表

[英]Iterating through all of a doubly linked list

I'm reading through a file and adding each line to a Doubly Linked List that I have, and I'm not sure how to include the last line of the file, and just hard-coded the last line. 我正在阅读文件,并将每一行添加到我拥有的双链表中,但我不确定如何包括文件的最后一行,而只是硬编码最后一行。 Is there a better way to do this? 有一个更好的方法吗?

(sc is a Scanner, s is the next String) (sc是扫描器,s是下一个字符串)

while(sc.hasNext()){
    if(s.trim().compareTo("") != 0)
        _list.addAtEnd(s);
    s = sc.nextLine();
}
_list.addAtEnd("A D 10");

Change your loop to first read the line and then add it : 更改循环以首先读取该行,然后添加它:

while(sc.hasNext()){
    s = sc.nextLine();
    if(s.trim().compareTo("") != 0)
        _list.addAtEnd(s);
}

If you have the first s = sc.nextLine() before the loop, remove it. 如果循环前有第一个s = sc.nextLine() ,请将其删除。

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

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