Implementing compareTo on linked list

Hey. Im trying to implement the compareTo method in java on a linked list. Not doing a very good job at it so hopeing for some help. Heres some code. Ive never done this so sorry if my questions sound retarted, or my code is way off.
Thanks
public class MutibleString implements Comparable<MutibleString>/*I want to compare 2 MutibleString's so is it best to put that in the<> or just not use these <>*/
{
/* Also do I need any spacific fields to implemtnt the Comparable interface right? these are what I have right now*/
         private Node head;
	private int size; 
	public char index;

/*
in here I have basic methods add remove charAt etc...
....
*/



public boolean compareTo(MutibleString that) 
	{
		if(this.MutibleString.compareTo(that.MutibleString))
		{
			return true;
		}
		return false;	
	}
}

Open in new window

theldroAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
anilallewarConnect With a Mentor Commented:
You are doing fine; the generics syntax is fine and you can use any arbitary fields for comparision...the compareTo() method is supposed to return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Suppose you say that if the size is what your comparision is based on (could be anything including elements in the Node class), you can have this code(remember it returns an int).

 public int compareTo(MutibleString that) {
        return this.getSize() - that.getSize();
    }
0
 
ksivananthCommented:
>> if(this.MutibleString.compareTo(that.MutibleString))

actually you need to compare the fields here!

you have three fields head, size, index, you need to decide which field or weight of each field and then compare them! for e.g., if you just want to comapre based on the size,

 return size - that.getSize() ;
0
 
theldroAuthor Commented:
Thanks
0
All Courses

From novice to tech pro — start learning today.