至少对我来说,我在大学里有一个棘手的练习要做。任务是用各种方法编写一个单链接列表.到目前为止还很容易,但现在的挑战是将这些单独链接的列表存储在一个链接列表中。在下面的文章中,您看到了我的单链接列表的实现,它实际上运行得很顺利:
public class Liste {
ListenElement first;
ListenElement last;
ListenElement current;
int count;
public Liste() {
    first = null;
    last = null;
    current = null;
    count = 0;
}
// Methods...单链列表由以下列表元素组成:
public class ListenElement {
String content;
ListenElement next;
public ListenElement(String content, ListenElement next)
{
    this.content = content;
    this.next = next;
}
//Methods...我的问题是:
LinkedList<Liste> zeilen = new LinkedList<>();
Liste zeile1 = new Liste();
Liste zeile2 = new Liste();
zeile1.addBehind("Hello");
zeile1.addBehind("World");
zeile2.addBehind("Hello");
zeile2.addBehind("World");
zeilen.add(zeile1);
zeilen.add(zeile2);
System.out.print(zeilen.get(1));
//Printed: Listen.Liste@4aa298b73 instead of Hello World.提前感谢您的帮助!
发布于 2015-04-26 08:40:25
System.out.print(zeilen.get(1)); //Printed: Listen.Liste@4aa298b73而不是Hello。
这是默认Object#toString的输出。如果希望从Liste类获得不同的输出,则需要重写toString以提供不同的输出。
例如:如果希望Liste#toString返回其内容的toString的逗号分隔列表:
@Override
public String toString() {
    StringBuffer sb = new StringBuffer(10 * this.count); // Complete guess
    ListenElement el = this.first;
    while (el != null) {
        sb.append(el.content.toString());
        el = el.next;
        if (el != null) {
            sb.append(", ");
        }
    }
    return sb.toString();
}(我在假设列表类是如何工作的,基于您展示的代码.)
https://stackoverflow.com/questions/29875459
复制相似问题