我正在尝试使用我附加的Person类中的对象创建链接列表,但我似乎无法打印我为每个对象创建的实际名称。我只能打印" gibberish"的典型.toString()输出。我还需要一种在使用Objects时打印出名称的方法。
public class personlink {
public static void main(String[] args)
{
LinkedList<Person> list = new LinkedList<Person>();
Person Alice = new Person("Alice");
Person Brady = new Person("Brady");
Person Cathy = new Person("Cathy");
Person Danny = new Person("Danny");
Person Amy = new Person("Amy");
Person Eddie = new Person("Eddie");
Person newp = new Person("New Person");
list.add(Alice);
list.add(Brady);
list.add(Cathy);
list.add(Danny);
//
System.out.println("Linked List Content: " + list);
//
list.addFirst(Amy);
list.addLast(Eddie);
//
System.out.println("LinkedList Content after addition: " + list);
//
list.removeFirst();
list.removeLast();
//
System.out.println("LinkedList after deletion of first and last element: " + list);
//
list.add(0, newp);
list.remove(2);
//
System.out.println("Final Content: " + list);
}
}
public class Person
{
String name;
public Person(String _name)
{
name = _name;
}
public String getName()
{
return name;
}
}
答案 0 :(得分:0)
修改Person
类添加toString()
方法。