【问题标题】:Adding a toString method to a linked list将 toString 方法添加到链表
【发布时间】:2013-04-11 14:59:29
【问题描述】:

我创建了一个链表类来制作一个简单的寄存器,我可以在其中从列表中添加和删除学生。但是我不确定如何为链表创建我的 toString 方法,最好的方法是什么? 提前致谢!

import java.util.*;
public class Registry {


LinkedList<Student> studentList
        = new LinkedList<Student>();
//setting my type parameter




public Registry() {}


public void addStudent(Student aStudent) {}


public void deleteStudent(int studentID) {}


public String toString(){}




public String format() {}


}

【问题讨论】:

  • 你想为每个链表对象显示什么?
  • 你知道如何使用StringBuilder吗?
  • 您可以打印列表中所有学生的详细信息,并带有一些格式。与 Collections 中的 toString() 相同。

标签: java linked-list tostring


【解决方案1】:

LinkedList 已经有一个继承自 AbstractCollection 的 toString() 方法。

toString

public String toString()

Returns a string representation of this collection. The string representation consists
of a list of the collection's elements in the order they are returned by its iterator,
enclosed in square brackets ("[]"). Adjacent elements are separated by the characters
", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

Overrides:
    toString in class Object

Returns:
    a string representation of this collection

这不是你想要的吗?

【讨论】:

  • 这将打印项目的哈希码列表。我认为提问者宁愿想要一个使用他们的 toString() 方法而不是 valueOf() 的项目列表。
【解决方案2】:

其意图似乎是列出存储在您的链接列表中的所有学生,而不是覆盖链接列表的toString()。只要你的Student 类覆盖了它的toString() 方法,你就在路上。打印链接列表将调用它的toString() 方法并给你你想要的。

覆盖 toString() 的示例类

class MyClass 
{
    private int x;
    private int y;

    /* getters and setters  */

    @Override
    public String toString()
    {
        return "MyClass [x=" + x + ", y=" + y + "]";
    }
}

用法

List<MyClass>  myList = new LinkedList<MyClass>();
MyClass myClass = new MyClass();
myClass.setX(1);
myClass.setY(2);
myList.add(myClass);
System.out.println(myList);

打印

[我的班级 [x=1, y=2]]

【讨论】:

    【解决方案3】:

    覆盖注册表类中的 toString()

    LinkedList 将调用成员对象的 toString()

    Source

    Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by java.lang.String.valueOf(java.lang.Object).

    【讨论】:

      【解决方案4】:
      1. 您没有创建链表类 - 您创建了一个 LinkedList 对象。
      2. 你想要的是 Registry 类的 toString() 方法

        公共字符串 toString() { 布尔括号添加 = false; StringBuffer 结果 = new StringBuffer();

        for(Student student : studentList) {
            result.append(bracketAdded ? ", " : "[");
            result.append(student);
            bracketAdded = true;
        }
        result.append("]");
        
        return result.toString();
        

        }

      现在剩下的就是为您的 Student 类实现一个 toString() 方法。

      【讨论】:

        【解决方案5】:

        我会使用 StringBuilder 并在列表中循环。

        public String toString(){
          StringBuilder sb = new StringBuilder();
          for (Student s: students){
            sb.append(s.toString()+",");
          }
          return sb.toString();
        }
        

        在 Student 类的 toString() 方法中包含您想要的任何信息。

        编辑:我在其他响应之一中注意到链表上的 toString() 正在使用 String.valueOf()。实际上,在大多数情况下,我更喜欢它,因为它将处理空值,除非您当然想知道空值何时出现在此列表中。

        所以而不是:

        sb.append(s.toString()+",");
        

        你可以使用:

        sb.append(String.valueOf(s),"+");
        

        【讨论】:

          猜你喜欢
          • 2021-02-07
          • 2017-07-29
          • 2010-09-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多