gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:50:20 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com