简体   繁体   English

从 LinkedList 创建 toString 输出

[英]Creating a toString output from a LinkedList

I've been trying to figure out what exactly the LinkedList is called and supposed to be called as within toString2.我一直在试图弄清楚 LinkedList 到底被调用了什么,并且应该在 toString2 中被调用。 I am still new to LinkedLists and don't quite understand them very clearly yet.我还是 LinkedLists 的新手,还不太清楚。 This is what I've figured out, not really sure where to go from here since I'm confused on the LinkedList' name.这就是我想出来的,因为我对 LinkedList 的名称感到困惑,所以不确定从这里去哪里。

public String toString2(){
    String output = "";
    
    while(node != null){
    }

    return output;
}

Where do I get the node from?我从哪里获得node That's the main thing that I'm confused about.这是我感到困惑的主要问题。

Original Problem ( Source )原始问题( 来源

Write a method toString2 that returns a string representation of the list, such as "[5, -2, 9]".编写一个 toString2 方法,返回列表的字符串表示形式,例如“[5, -2, 9]”。 Assume that you are adding this method to the LinkedIntList class as defined below:假设您要将此方法添加到如下定义的 LinkedIntList 类:

public class LinkedIntList {
    private ListNode front;   // null for an empty list
    ...
}

Maybe you can try this to iterate and get the values?也许你可以试试这个来迭代并获取值?

  public String toString2(){
    String str = "";
    Node n = first;
    while( n != null ){
        str = str + n.getValue() + " ";
         n = n.getNext();
    }
    return str;

unless you provide complete code it would be hard to know what actual changes need to be made.除非您提供完整的代码,否则很难知道需要进行哪些实际更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM