简体   繁体   中英

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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