0

I have a LinkedList

private LinkedList<Node> vertices;

Here is the class Node

public class Node 
{
 String value;
 linkedList edges ;
 public Node()
 {
 value=null;
 edges=new linkedList();
 }
}

Now the LinkedList i mentioned above contains the nodes in my graph.What i want to do is pass a string value to a method .This method should check if the nodes in the LinkedList contains a Node which has a value equal to the value i passed .If so it should return the index of the Node.

Here is what i tried to do

public Node getNode(String value)
 {
 int index=vertices.indexOf(value);//this is where the problem is.
 //index is getting assigned a value -1 
 return vertices.get(index);
 }

and called the method this way

temp2=getNode(header[dest]);

But the call to the method vertices.indexOf(value) is returning -1 (-1 shows it does not have it . although it has a node which has a value equal to the value i passed) . how do i check for the nodes which matches the value with the value i pass . Vertices is of type node and i am passing a string value.

asked Apr 21, 2015 at 9:58
2
  • The indexOf method is used to locate a character or string within another string. indexOf returns an integer whose value is the location in the string where your substring starts. It returns -1 if the substring is not found. So if you are looking for "abc" and a node contains "xabc" you get a match. If a node contains "yaleabcistheone" you also get a match. I believe you stated that you wanted the string to match exactly so you want to use ".equals" instead of indexOf** Commented Apr 21, 2015 at 10:27
  • this is not the problem . the problem is i am sending an string value to the indexof method . it is unable to compare a string with a Node.either i send a Node instead of the string value or i need some mechanism for camparing a Node with a string value. Commented Apr 21, 2015 at 10:30

3 Answers 3

1

In your node you need to implement equals method as indexOf method of LinkedList uses equals method internally to compare the Node that you passed with other existing nodes in List like:

public boolean equals(Node otherNode) {
 //... compare node values
}

You need method as:

 public Node getNode(Node value)//or create node internally with string that you get as input parameter
 ^^^^^
answered Apr 21, 2015 at 10:02
1
  • I can not send a Node value for comparision . i need to send a string value only Commented Apr 21, 2015 at 10:32
1

As per the JavaDocs, the indexOf method internally makes use of the equals method, which you do not seem to be overloading. Thus, you are trying to compare a String with a Node object.

To fix this, you will need to override the equals(Object obj) method in your Node class.

Something like so:

@Override
public boolean equals(Object obj) {
 if(obj instanceof Node) {
 Node comp = (Node)obj;
 return comp.value.equals(this.value);
 }
 else if(obj instance of String) { 
 String comp = (String)obj;
 return comp.equals(this.value)
 }
 return false;
}
answered Apr 21, 2015 at 10:03
1
0
 private Node getNode(int index) {
 if (index < 0 || size <= index) {
 throw new IndexOutOfBoundsException("index = " + index + " and should be between 0 and size: " + size);
 }
 if (index == 0) {
 return head;
 }
 Node node = head.next;
 int i = 1;
 while (i < index) {
 node = node.next;
 ++i;
 }
 return node;
 }
Sachith Muhandiram
2,97811 gold badges54 silver badges113 bronze badges
answered May 22, 2022 at 23:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.