I'm having trouble to compare two string from my LinkedList. I took me 2 days now trying figure out how to compare the current string to previous string in the linkedlist. Please help me out. Thank in advance. Here is my code.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I'm having trouble to compare two string from my LinkedList. I took me 2 days now trying figure out how to compare the current string to previous string in the linkedlist. Please help me out. Thank in advance. Here is my code.
Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.
The compareTo() method you posted doesn't compare the current object's data to the previous object's. Are you asking how to change the method you posted to do that, or would you prefer writing a new method that does what you've asked?
Something to consider, a linked list typically has a pointer to the next item in the list, not the previous, so you'd have to create an iterator to find the item in the list previous to the current object and then do the comparison. A doubly linked list would also have a pointer to the previous object, making the solution somewhat easier, but either way is doable.
sydethsam (March 2nd, 2014)
Thank for reply. Either change my method or writing a new method is work for me. The thing is I'm not really good at Linkedlist at all. Here is my whole LinkedListNode class. Please let me know if my question make you confuse
public class LinkedListNode { private String data; private LinkedListNode next; public LinkedListNode(String data) { this.data = data; this.next = null; } public String getData() { return data; } public LinkedListNode getNext() { return next; } public void setNext(LinkedListNode n) { next = n; } public int compareTo(LinkedListNode n){ //Compare two string String myHead = data.toLowerCase(); String comparableHead = data.toLowerCase(); return (myHead.compareTo(comparableHead)); } }
As the code goes through the links in the linked list going from one node to the next node, it needs to save the link to the previous node to be able to compare the contents of the current node to the previous node.how to compare the current string to previous string
If you don't understand my answer, don't ignore it, ask a question.
sydethsam (March 2nd, 2014)
could you provide a small code how to save link to previous node? I'm trying to thinking in my head, but I just have no idea. I try to look to google too, but still no helpful source code. Thank in advance.
One more thing, how do I avoid to get ban from asking question in here, because I got ban from stackoverflow.com, they said that I do have low reputation.
"I'm trying to think, and nothin' happens." - Curly Howard
For a method:
compareTo( Node node )
where 'node' is the current node's previous node:
1. Find the current node's previous node - this is the hard part
A. Set Node temp = head
B. If temp.next == current, temp is previousNode in Step 2
C. Else temp = temp.next, return to B
D. If here, there is no previousNode
2. Call compareTo( previousNode )
sydethsam (March 3rd, 2014)