我目前正在构建一个程序,它需要我使用链表的链表,我已经创建了我自己的链表,但我不知道如何用链表替换我的对象。我现在的代码是:
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();
}
}
我没有使用过泛型,因为我以前从未使用过它们,也不知道如何使用
我只是展开这里的注释:
这就是如何创建一个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;