java - 将 toString 方法添加到链表中

标签 java linked-list tostring

我创建了一个链接列表类来进行简单的注册,我可以在其中添加和删除列表中的学生。但是我不确定如何为链接列表创建 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() {}


}

最佳答案

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

这不是你想要的吗?

关于java - 将 toString 方法添加到链表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16083914/

相关文章:

java - selenium webdriver 获取表中的可见行号

java - 使用 lambda 的可运行线程

java - View 排序和过滤 : GlazedList sorting and filtering + JTable vs Glazed event list+JXTable

c - 我的与链表有关的 C 程序出了什么问题?

java - 在 toString 类中使用 for 循环返回多个数组

java - toString.equals 不起作用?

asp.net-mvc - MVC Razor 获取月份名称

java - RMI 中命名和注册表的区别

c - 如何将链表中的最后一个节点复制到变量中,然后删除链表中的原始节点?

c++ - 链表功能调用