David Cosman
David Cosman

Reputation: 433

Is there a way to search for something with an "any" parameter in a Stack?

I'm using a stack to store the edges of a Graph implementation I'm doing. I made a getEdge(Node u, Node v) method that searches for a specific edge and returns it if it's found. I thought I would just use the search(obj o) included in the stack to search for the Edge

The problem I'm facing is that the Edge has the parameters (Node u, Node v, String type) in it. For the string parameter, I don't specifically care what it is. It could be any string and should return it if it finds the edge;

public Edge getEdge(Node u, Node v)
{
...
    Edge temp = new Edge (u, v, /*here is where the string goes*/);
    return edges.search(temp);
}

Again, I don't care what String has stored in it; I just care if the nodes u and v are connected in an edge in the stack. Is there a way to put a string value in Edge like "any"?

Upvotes: 0

Views: 44

Answers (2)

bellum
bellum

Reputation: 3710

During Stack.search() equality of Edge objects is checked using method equals() of Edge class. So you must override this method with no accent on String field of Edge.

Upvotes: 1

amit
amit

Reputation: 178491

You can create a dummy Edge with an empty string as a parameter and search for it.

This of course assumes your overiden equals() method does NOT use the string.

edges.search(new Edge(u,v,""));

If the equals() does use the string - you will probably need to iterate the stack, something along the lines of:

for (Edge e: edges) { 
    if (e.first().equals(u) && e.second().equals(v)) return e;
}
return null;

Upvotes: 0

Related Questions