I have a project where i am having to create a doubly link list of persons, with lastname, firstname, and id number.

Basically, how do I implement my compareTo method in my add method of dbl list ???

thanks in advance.

Here is my person class:

public class Person implements Comparable<Person>{

	String firstName;
	String lastName;
	int idNum;
	//getters and setters for Person
	public String getFirstName() {
		return firstName;

	public void setFirstName(String firstName) {
		this.firstName = firstName;

	public String getLastName() {
		return lastName;

	public void setLastName(String lastName) {
		this.lastName = lastName;

	public int getIdNum() {
		return idNum;

	public void setIdNum(int idNum) {
		this.idNum = idNum;

	//constructor for Person
	public Person(String firstName, int idNum, String lastName) {
		this.firstName = firstName;
		this.idNum = idNum;
		this.lastName = lastName;

	//compareTo method for object Person
	public int compareTo(Person o) {
		if(this.idNum < o.getIdNum()){
			return -1;
		} else if (this.idNum == o.getIdNum()){
			return 0;
		} else 
			return 1;


Here is my Node class:

class Node<E> {
	Node(E value) {
  		this.value = value;
 	Node(E value, Node<E> prev, Node<E> next) {
  		this.value = value;
  	void setPrev(Node<E> prev) {
  		this.prev = prev;
  	void setNext(Node<E> next) {
  		this.next = next;
  	Node<E> getPrev() {
  		return prev;
  	Node<E> getNext() {
  		return next;
  	E getValue() {
  		return value;
  	private E value;
  	private Node<E> prev;
 	private Node<E> next;

And finally, here is my SortedDblList class

public class SortedDblList <E extends Comparable <? super E>> {

	private Node<E> head = new Node<E>(null);
	private Node<E> tail = new Node<E>(null);
	private int size = 0;

	public Node<E> get(int index) throws IndexOutOfBoundsException {
		if (index < 0 || index > size) {
			throw new IndexOutOfBoundsException();
		} else {
			Node<E> cursor = head;
			for (int i = 0; i < index; i++) {
				cursor = cursor.getNext();
			return cursor;

	public boolean add(E element){
		//if isEmpty == true, then head == element
		//if head = NULL add there.
		Node<E> cursor = head;
		Node<E> temp = new Node<E>(element);
		for (int i = 0; i < size; i++) {
			cursor = cursor.getNext();
			//((Comparable<? super E>) cursor).compareTo(temp);
			cursor.compareTo(element);            //error

I might have some more questions here so thanks in advance for anyone willing to help.

8 Years
Discussion Span
Last Post by jkun2

Nevermind, I realized im pretty sure all i had to was call cursor.getValue().compareTo(element).... not sure why i didnt think of that in the first place...

Maybe someone can help me with this, we have a toString method in our Person class such as

public String toString() {
		return idNum + " " + firstName + " " + lastName;

and then a class called Student with a string variable for the college, my class as of right now is

public class Student extends Person{
	public Student(String firstName, int idNum, String lastName) {
		super(firstName, idNum, lastName);
		// TODO Auto-generated constructor stub

	String college;
	public String getCollege() {
		return college;

	public void setCollege(String college) {
		this.college = college;

	public String toString(){
		String result = Person.toString();
		return Person.toString() + " [" + college + "]"; 
/*Having trouble with toString here*/

Basically, the result should be: 1111 Joe Smith [Art&Sci]

how do i append the college string of class Student to the result of Person.toString in the toString method of Student??


If you just want to append to the string generated by the parent class toString() method, use this

return super.toString() + " [" + college + "]";

: ( Yeah i rushed to put that without thinking, I am retarded. Thank you...


Maybe this question isn't so trivial...

Here is the description for the print method:

The printList method will print all elements of the list to the screen; one element per line. That is, for each element of the list it should call the toString method of the element, and then append a newline character to the result, and print the result to the screen. As an example, a list of two Integers 3 and 4 should be printed to the screen as follows:

public void printList();

How do i make this method iterate through and print? do i need to use a StringBuffer and append a newline character after each iteration through? Then do getValue and pass that value to toString of Person?

sorry about all the questions, just helps a lot.

Also, there is a remove method that takes in an Object o, so a node, and how do i compare this to the data?

lets say i made,

Node<E> cursor = head;

then did cursor.equals(o), would that accurately compare the nodes? and then if match, i could just re-route the setPrev and setNext?

thanks a lot

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.