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