我目前正在构建一个程序,要求我使用链接列表的链接列表,我已经创建了自己的链表,但我不知道如何用链表替换我的对象。我目前的代码是:
import java.io.Serializable;
class ListNode implements Serializable {
private static final long serialVersionUID = 8937086815484947032L;
private Client payload;
private ListNode next;
protected ListNode(Client payload) {
this.payload = payload;
this.next = null;
}
protected void setNext(ListNode next) {
this.next = next;
}
protected ListNode getNext() {
return next;
}
public String toString() {
return payload.getClient();
}
}
public class OrderedLinkedList implements Serializable {
private static final long serialVersionUID = 7651599382448755370L;
private ListNode head;
public void add(String givenName, String surname) {
ListNode newNode = new ListNode(new Client(givenName, surname));
if (head != null)
newNode.setNext(head);
head = newNode;
}
public String toString() {
ListNode iter = head;
String collector = "";
while (iter != null) {
collector = collector + iter.toString();
iter = iter.getNext();
}
return collector;
}
}
如何制作此链接列表,显示另一个链接列表,然后显示对象客户端。这是对象客户端的代码
import java.io.Serializable;
public class Client implements Serializable {
private static final long serialVersionUID = 6472982140162477886L;
String givenName;
String surname;
public Client(String givenName, String surname) {
this.givenName = givenName;
this.surname = surname;
}
public String getClient() {
StringBuilder sd = new StringBuilder();
sd.append("First Name: ").append(givenName).append("\tSurname: ").append(surname).append("\n");
return sd.toString();
}
}
我没有使用泛型,因为我以前从未使用过它们而且不知道如何
答案 0 :(得分:0)
我只是在这里展开评论:
这就是你如何制作LinkedLists的LinkedList:
List<List<Client>> listOfLists= new LinkedList<List<Client>>();
添加新列表:
listOfLists.add(new LinkedList<Client>());
添加元素:
listOfLists.get(0).add(new Client());
不要忘记进口:
import java.util.List;
import java.util.LinkedList;