As part of an assignment I have been asked to create a SortedLinkedList class. We have been told that we must use the java.util.LinkedList<E> in order to dervie this class.
I understand that I must in some way overwrite the .add(Object) method.
I realise that I must in some way make a call to super.add(Object), however, this gives me nothing to carry out Collections.sort() method on.
My code at present is as follows:
import java.util.*;
/**
* Creates a Sorted Linked List in Ascending Order
*
* @author Tussles
* @version 14/10/09
*/
public class SortedLinkedList<E> extends LinkedList<E>
{
/**
* Constructor for objects of class SortedLinkedList
*/
public SortedLinkedList()
{
}
//**Overwriting the add method here**//
public boolean add(E c)
{
super.add(c);
return true;
}
}
EDIT: Including the main code I am running to test this Class. User is the object I am attempting to store within a Sorted Linked List.
public static void main(String[] args)
{
User user1 = new User("Tuss","Les",0);
User user2 = new User("Zed","Zeberdee",0);
User user3 = new User("Macy","Middle",0);
SortedLinkedList LibraryUser = new SortedLinkedList();
LibraryUser.add(user2);
LibraryUser.add(user1);
LibraryUser.add(user3);
for (int i = 0; i<LibraryUser.size();i++)
{
System.out.println(LibraryUser.get(i));
}
}
This is the output results at present (I am adding, but not sorting the list)
First Name: Zed Second Name: Zeberdee
First Name: Tuss Second Name: Les
First Name: Macy Second Name: Middle
How would I go about altering this so that I am able to sort the List every time I add a new Object to it?
If more information is needed I can provide it.
Thanks