File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Printing out a linked list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing out a linked list" Watch "Printing out a linked list" New topic
Author

Printing out a linked list

Josh Theisen
Ranch Hand

Joined: Sep 23, 2011
Posts: 31
Hey guys, I'm back again!

Alright, here's what I've got going on: I've got to add items (integers) to a linked list. I'll keep adding integers until I feel I have enough in there.
When -1 is entered, the loop stops and I need to then print out the linked list.

So far I've gotten the input part down just fine (I'll still have you take a look at it since I'd like feedback on how I decided to do it) but it's the print that I'm having issues with.

Here's what I've got:

Declares a new LinkedList


Asks for input until -1 is entered.


Now I'd like to create a print method... something that will print out what the list has in it.
Note that I'd like to just get some ideas of how to get the ball rolling with this method...I'm not a fan of just getting
the answer and pasting that into my program.

Thanks in advance!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

First investigate whether the toString() method of LinkedList already does what you want. If it's okay, then just use that.

Otherwise just iterate through the list and print each of the elements.

Of course it would help if you had some idea of what you wanted to print, before you started to write any code.
Josh Theisen
Ranch Hand

Joined: Sep 23, 2011
Posts: 31
Is there any reason why it'd be bad to approach it like this? The reason it's named "unsorted" is because I'll have to do another method in which linked list is sorted...


Also, ignore the fancy system out ;)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Two things:
  • You ought to print something like " ... -1 to exit."
  • You do not have a method there. You have a method which fills a list, and a method which prints it, and you are pretending both are the same method. You ought to have two methods.
  • Josh Theisen
    Ranch Hand

    Joined: Sep 23, 2011
    Posts: 31
    Thanks for the replies - I"ll be posting another thread sometime regarding a different question I have. But this thread can be locked/ignored as it was a dumb thread anyway.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Printing out a linked list
     
    Similar Threads
    Ready to give up!
    Another beginner problem...
    LinkedList
    Sooo Close (Arrays & exit loop w/ 0 )
    Linked List - inserting value to correct position