How to implement or extend lists for creating my own lists
i need to create a LinkedList of my own object.
how do i make proper or intelligent use of the LinkedList class/interface and the ListNode class/interface.
for example, if i have an Object of my own say :
class Person{
private String name;
private Date date_of_Birth;
private String address_1, address_2;
privatelongint contact_no;
}
in the above case how do i create a list of Persons using LinkedList and ListNode, without using the following manner :
LinkedList <Person> personList =new LinkedList <Person> ();
The "old-fashioned" way is:Person myPerson = new Person(....);
LinkedList myList = new LinkedList(); // list without generics can take any kind of object
myList.add(myPerson);
... etc ...
Person retrievedPerson = (Person)myList.get(index); // note: casting needed without generics
If this is not what you mean, please explain more of what you want. Also please explain why you don't want to use generics.
well, Herko,
what i want is to do the following :
class Person extends ListNode {
--
--
--
}
OR
class Person implements ListNode {
--
--
--
}
and then use this ListNode as a node of the LinkedList.
Also then i want to sort this Linked List according to the 'date_of_birth' attribute of the Person.
Plz tell me if u want anything more.
Bad design smell. Person IS-A ListNode? I highly doubt it, so your Person class shouldn't extend or implement ListNode.
As ListNode isn't a class from the standard API's, I guess this is some kind of assignment where you have to make your own LinkedList, right?
In that case, your ListNode should contain getters and setters for a field of type Object so that you can store a Person (or anything else) in the ListNode:public class ListNode {
private Object m_data;
public void setData(Object data) {
m_data = data;
}
public Object getData() {
return m_data;
}
...
}