简体   繁体   中英

Node to String: Changing the String to Reverse

The problem I am having is that I making a Node into a string and I have accomplished that much but what I am having trouble is now making it show in reverse. Its a list and I have tried a few methods but it keeps showing the order as if it wasn't in reverse. A code example would be appreciated.

Here is my CODE:

public void reverseDisplay(){       
    Node currentNode = head;
    while(currentNode != null){
        String out = "";
        out = out + currentNode.getItem() + " ";
        System.out.print(out);
        currentNode = currentNode.getNext();
    }
    System.out.println();
}

You were on the right track (untested but should work):

public void reverseDisplay(){       
  Node currentNode = head;
  String out = "";

  while(currentNode != null){

    out = currentNode.getItem() + " " + out; // instead of out + currentNode.getItem() + " ";
    currentNode = currentNode.getNext();

  }
  System.out.println(out);
}

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