[英]Node to String: Changing the String to Reverse
我遇到的問題是,我將Node制成字符串,並且已經完成了很多工作,但是現在遇到的麻煩是使它反向顯示。 它是一個列表,我已經嘗試了幾種方法,但是一直在顯示順序,好像不是相反的順序。 代碼示例將不勝感激。
這是我的代碼:
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();
}
您處在正確的軌道上(未經測試,但應該可以工作):
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.