Results 1 to 4 of 4
  1. #1
    sirdonclemo is offline Member
    Join Date
    Aug 2010
    Location
    US
    Posts
    4
    Rep Power
    0

    Default Problem with ToString list (java)

    i am a hard time for the toString in this program

    Java Code:
    public class SpecializedList implements SpecializedListInterface
    {
      protected class SListNode
      // List nodes for the specialized list implementation
      {
        protected byte info;        // The info in a list node
        protected SListNode next;   // A link to the next node on the list
        protected SListNode back;   // A link to the previous node on the list
      }
     
      protected SListNode listFirst;      // Reference to the first node on list
      protected SListNode listLast;       // Reference to the last node on the list
      protected int numElements;          // Number of elements in the list
      protected SListNode currentFPos;    // Current forward position for iteration
      protected SListNode currentBPos;    // Current backward position for iteration
     
      public SpecializedList()
      // Creates an empty list object
      {
        numElements = 0;
        listFirst = null;
        listLast = null;
        currentFPos = null;
        currentBPos = null;
      }
    
      public int size()
      // Determines the number of elements on this list
      {
        return numElements;
      }
    
      public void resetForward()
      // Initializes current forward position for an iteration through this list
      {
        currentFPos = listFirst;
      }
     
      public byte getNextElement ()
      // Returns the value of the next element in list in forward iteration
      {
        byte nextElementInfo = currentFPos.info;
        if (currentFPos == listLast)
          currentFPos = listFirst;
        else
          currentFPos = currentFPos.next;
     
        return nextElementInfo;
      }
     
      public void resetBackward()
      // Initializes current backward position for an iteration through this list
      {
        currentBPos = listLast;
      }
     
      public byte getPriorElement ()
      // Returns the value of the next element in list in backward iteration
      {
        byte nextElementInfo = currentBPos.info;
        if (currentBPos == listFirst)
          currentBPos = listLast;
        else
          currentBPos = currentBPos.back;
     
        return nextElementInfo;
      }
    
      public void addFront (byte element)
      // Adds the value of element to the beginning of this list
      {
        SListNode newNode = new SListNode();   // Reference to node being added
        newNode.info = element;
        newNode.next = listFirst;
        newNode.back = null;
        if (listFirst == null)            // Adding into an empty list
        {
          listFirst = newNode;
          listLast = newNode;
        }
        else                             // Adding into a non-empty list
        {
          listFirst.back = newNode;
          listFirst = newNode;
        }
        numElements++;
      }
    
      public void addEnd (byte element)
      // Adds the value of element to the end of this list
      {
        SListNode newNode = new SListNode();   // Reference to node being added
        newNode.info = element;
        newNode.next = null;
        newNode.back = listLast;
        if (listFirst == null)            // Adding into an empty list
        {
          listFirst = newNode;
          listLast = newNode;
        }
        else                             // Adding into a non-empty list
        {
          listLast.next = newNode;
          listLast = newNode;
        }
        numElements++;
      }
      
      public String toString ()
      {
          String listString = " List: \n";
          SListNode current = listFirst;
          while (current != null)
          {
              listString += current;
              current = current.back;
            }
            return listString;
        }
          
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sirdonclemo View Post
    i am a hard time for the toString in this program
    Please elaborate -- tell us how exactly are you having a hard time -- so we can know how to help you.

  3. #3
    sirdonclemo is offline Member
    Join Date
    Aug 2010
    Location
    US
    Posts
    4
    Rep Power
    0

    Default

    i am trying to print out the item in my list but instead i get the addresses printed out

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Then you need to implement a toString method in the SListNode class as well.

Similar Threads

  1. java problem with list
    By mike_ledis in forum AWT / Swing
    Replies: 3
    Last Post: 03-13-2011, 02:40 PM
  2. how to create list of list in java ???
    By ilayaraja in forum Advanced Java
    Replies: 1
    Last Post: 10-26-2009, 05:30 PM
  3. Replies: 2
    Last Post: 10-23-2009, 03:48 AM
  4. toString() method of java.security.MessageDigest
    By Nicholas Jordan in forum Advanced Java
    Replies: 4
    Last Post: 09-18-2008, 03:32 PM
  5. problem with super.tostring()
    By faw in forum Advanced Java
    Replies: 1
    Last Post: 05-01-2008, 05:27 PM

Posting Permissions

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