I'm currently building a program which requires me to use a Linked List of Linked Lists, and I have created my own linked list, but I don't know how to replace my object with a linked list. My current code is:
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;
}
}
How do I make this linked list, show another linked list which then shows the object client. This is the code for object client
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();
}
}
I haven't used generics because I've never used them before and don't know how
I'm just unfolding the comments here:
That is how you make a LinkedList of LinkedLists:
List<List<Client>> listOfLists= new LinkedList<List<Client>>();
Adding a new list:
listOfLists.add(new LinkedList<Client>());
Adding an element:
listOfLists.get(0).add(new Client());
And don't forget the imports:
import java.util.List;
import java.util.LinkedList;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.