First of all, I'd like to thank you for that time when you helped you, I really appreciate this.
So I hope again for some help from you if you can please ...
I am doing LinkedStack and almost methods are working, I've tested in JUnit, and they are working
but the IndexOf method is not working, and I googled for a solution and nothing, so you'll see below my variant how I've tried and hope you can help me with this .
Thanks in advance ;)
NEVER MIND. My error.
I've tried but it gives syntax error
We have this method:
I assume that T is a generic type.
Then we have:
So, top.getElement can be cast to an int, but I can't place that with the line following, where element of type T is tested for equality against something that can be cast to an int.
Is T a bound generic type? If so, what is the bound?
And what is top?
Can you show us the LinearNode<T> class?
Until then, I can only assume that top.getElement() should deliver the T value in it, not something that can be cast to an int,
But first yet another question: can you also show us the full code of the LinkedStack<T> class? I like to see how variables of type T are added and, formost, removed (how does the stack retrieve the previous top?)
so yeah as I said before hope you can help me to find a solution for this
Seems like you'd need something like
Carey Brown wrote:LinearNode node = top;
for( i=0 ; i < count && ! ((element == null && node.element == null) || node.element.equals(element)) ; i++, node = node.getNext() )
if( i == count )
Edit: Note that this method only works if T has a usefull equals method.
Piet Souris wrote:for( i=0 ; i < count && ! ((element == null && node.element == null) || node.element.equals(element)) ; i++, node = node.getNext() )
Or to simplify a bit:
Does the index of an element on a stack have any meaning? You look at the top element (or top 2‑3) and would not usually seek elements a long way down the stack.
Congratulations. I'm glad this worked for you. If you really do have data with null in it you'll need to incorporate Paul's suggestion.
Constantin Cornea wrote:Finally, the only change that I did is just node.getElement() as you can see below :
A minor note: Having a trailing semi colon at the end of a for() loop is often a bug. In this case it is not and to emphasize this I put it on its own line below the for() to show that it's an empty statement. You could do the same thing (perhaps better) with an empty set of braces.
I think you should go one further: add a comment saying // empty statement or similar.
Carey Brown wrote:. . . show that it's an empty statement. You could do the same thing (perhaps better) with an empty set of braces.