Results 1 to 5 of 5
  1. #1
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default getting first item in a linkedList

    I have the method below that is supposed to return the first item in a doubly linked list:

    Java Code:
    private Node<T> head;	 
    
    public Node<T> returnHead() {
    	 	return head;
    	 }
    I then call the method like this:

    Java Code:
    System.out.println("Head = " + myLinkedList.returnHead());
    I don't get any errors, but I get this strange string returned:
    Head = myLinkedList$Node@42e816

    I'm not sure exactly what this is I'm getting. The items in the list are a list of books(String).

    Any help would be greatly appreciated.

    Thanks!

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    It's because the Node<T> object has no good toString() method, so it returns what appears to be gibberish. (It's really not, but I'm not sure you really want to know what it means...)

    So, you can either give all your nodes in there a toString() method that displays something usable, or pick specific member variables from it to display instead.

  3. #3
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    Thanks...for a toString method I just need to convert it to string? .NET has a toString function built-in...how does one do that in Java?

    Thanks again

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Every object in java has toString method (is used by println statements to print the object), which by default does what the API says it does (see Object (Java Platform SE 6) ). Override this function if you wish this method to returns something different

  5. #5
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    Oh ok that makes sense.

    I just want the toString method to return the actual string contained in the list, nothing fancy...so I guess I can just use the built-in method.

    Thanks!

Similar Threads

  1. I need help in LinkedList ...
    By Usman in forum Advanced Java
    Replies: 3
    Last Post: 03-25-2011, 01:38 AM
  2. NullPointer adding an item to a LinkedList
    By sehudson in forum New To Java
    Replies: 7
    Last Post: 03-10-2011, 03:39 AM
  3. LinkedList
    By [RaIdEn] in forum New To Java
    Replies: 7
    Last Post: 10-13-2009, 12:59 AM
  4. LinkedList help
    By jigglywiggly in forum New To Java
    Replies: 6
    Last Post: 09-19-2009, 07:24 AM
  5. how to use LinkedList
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 01:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •