【问题标题】:Java toString printing error with a linkedlist using objects使用对象的链表的 Java toString 打印错误
【发布时间】:2017-04-20 04:29:19
【问题描述】:

我会尽量简化。 我有一类 Pet 对象:

public class Pet {
private String name;
private int treats;
private Coordinate coor;
}

请注意,coor 只是一个包含 2 个浮点数、经度和纬度的对象。

在我的可执行类中,我调用了我的 PetList(a linkedlist) 类的 printAll() 函数。

PetList List = new PetList();
... // adding nodes to my linkedlist etc.
...
List.printAll();

在我的 printAll() 函数中,我在我的 Pet 类中调用我的 toString 函数:

public void printAll() {
    LinkNode temp = first;
    while(temp != null) {
        System.out.print(toString());
        System.out.println();
        temp = temp.next;
    }
}

在我的 toString() 函数中,我创建了一个 StringBuilder:

// Most of this appending is formatting
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(name);
    // sb.append("{");
    sb.append(treats);
    // sb.append("} ");
    //if(coor.getLatitude() >= 0) {
    //    sb.append("+");
    //}
    sb.append(coor.getLatitude());
    // sb.append(" ");
    //if(coor.getLongitude() >= 0) {
    //    sb.append("+");
    //}
    sb.append(coor.getLongitude());
    return sb.toString();
}

我正在读取输入直到 EOF:

add 3.0 3.0 Three // add(adds a node) with 2 floats (longitude & latitude) and Three(a name)
add 2.0 2.0 Two
add 1.0 1.0 One

但是当我调用 print(调用 printAll())时,我得到了这个:

add 3.0 3.0 Three
add 2.0 2.0 Two
add 1.0 1.0 One
print
PetList@33909752
PetList@33909752
PetList@33909752

但我希望这种情况发生:

add 3.0 3.0 Three
add 2.0 2.0 Two
add 1.0 1.0 One
print
One{0} +1.0, +1.0
Two{0} +2.0, +2.0
Three{0} +3.0, +3.0

我不确定,但我得到的是参考而不是任何实际数据吗?是由于我显示的代码中的任何内容吗?还是在别的地方?感谢任何帮助!

编辑:

Here is the beginning of my PetList class:
public class PetList {
    protected class LinkNode {
        protected LinkNode next;
        protected LinkNode prev;
        protected Pet animal;

        public LinkNode() {
            next = prev = null;
        }
    }

这是我现在调用 toString 函数的方式:

//public void printAll() {
 //  LinkNode temp = first;
 //   while(temp != null) {
        System.out.print(animal.toString());
 //       System.out.println();
 //       temp = temp.next;
 //   }
//}

这是我在尝试编译时遇到的错误:

PetList.java:36: error: cannot find symbol
            System.out.print(animal.toString());
                             ^
  symbol:   variable animal
  location: class PetList

【问题讨论】:

  • 你的 PetList 类有自己的 toString() 方法吗?现在它正在使用 Object.toString() 方法。
  • 哦,天哪,它没有,所以我一直没有调用 toString 方法,哈哈
  • 嗯,在我的 PetList 类中,我有一个 protected Pet animal;,但是当我将动物添加到我的 toString 调用时,它给了我一个错误。修改调用:System.out.print(animal.toString()); 错误:./PetList.java:37: error: cannot find symbol System.out.print(animal.toString());
  • @Jason 有什么想法吗?
  • 使用上面的 sn-p 更新可能会有所帮助。

标签: java io reference linked-list


【解决方案1】:

基于你最新的代码sn-p:

        System.out.print(animal.toString());

...应该是

        System.out.print(temp.animal.toString());

因为animalLinkNode 的实例变量,而不是PetList

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多