• Post Reply Bookmark Topic Watch Topic
  • New Topic

IndexOF Method for LinkedStack  RSS feed

 
Constantin Cornea
Ranch Hand
Posts: 40
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI, Everyone!!
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 ;)

 
Carey Brown
Saloon Keeper
Posts: 3259
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you initialize 'i' in the for loop you are already subtracting 1, don't subtract 1 again in the return statement.
NEVER MIND. My error.
 
Carey Brown
Saloon Keeper
Posts: 3259
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your loop is decrementing 'i' but I don't see where you are iterating to the next element.
 
Constantin Cornea
Ranch Hand
Posts: 40
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if in the for loop I will put top.getNext() then Java says that cannot cast from LinearNode ...

I've tried but it gives syntax error
 
Constantin Cornea
Ranch Hand
Posts: 40
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, the class for LinkedStack is looking like this :

 
Piet Souris
Rancher
Posts: 2019
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a little unclear to me.
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?
 
Constantin Cornea
Ranch Hand
Posts: 40
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the top is type of LinearNode class as I posted above
 
Constantin Cornea
Ranch Hand
Posts: 40
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes the T is just generic type not bounded generic type
 
Piet Souris
Rancher
Posts: 2019
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, your post was a couple of seconds before mine.
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,
 
Constantin Cornea
Ranch Hand
Posts: 40
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the Linear Node class is given by my teacher and here it can't be some mistakes

 
Piet Souris
Rancher
Posts: 2019
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm. I have some questions about this,
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?)
 
Constantin Cornea
Ranch Hand
Posts: 40
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes sure

 
Constantin Cornea
Ranch Hand
Posts: 40
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
inside the IndexOF method, which I am in trouble I've modified a little the statement with equals but still is not working properly

so yeah as I said before hope you can help me to find a solution for this
 
Carey Brown
Saloon Keeper
Posts: 3259
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the "index" starting from top or the bottom?

Seems like you'd need something like
 
Carey Brown
Saloon Keeper
Posts: 3259
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No sure why you need an indexOf() method, you don't have any methods that take an index as an argument. You don't have get(index) or peek(index) for instance.
 
Piet Souris
Rancher
Posts: 2019
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the T element can be null, the code needs a slight adjustment:
Carey Brown wrote:LinearNode node = top;
int i;
for( i=0 ; i < count && ! ((element == null && node.element == null) || node.element.equals(element)) ; i++, node = node.getNext() )
    ;
if( i == count )
    return -1;
else
    return i;[/code]

Edit: Note that this method only works if T has a usefull equals method.
 
Paul Clapham
Sheriff
Posts: 22713
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Piet Souris
Rancher
Posts: 2019
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed much simpler! Had not seen this method before, thanks for pointing out.
 
Constantin Cornea
Ranch Hand
Posts: 40
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown thank so much for your help ;-), I've tried your given solution and is working as you can see below from the code and also a screen of Junit Test

JUnit_Test_IndexOF.JPG
[Thumbnail for JUnit_Test_IndexOF.JPG]
 
Constantin Cornea
Ranch Hand
Posts: 40
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally, the only change that I did is just node.getElement() as you can see below :

 
Constantin Cornea
Ranch Hand
Posts: 40
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank so much ;)
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't double‑space your code throughout. Use empty lines to separate logical parts, not successive lines.
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.
 
Carey Brown
Saloon Keeper
Posts: 3259
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constantin Cornea wrote:Finally, the only change that I did is just node.getElement() as you can see below :

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.

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.
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:. . . show that it's an empty statement. You could do the same thing (perhaps better) with an empty set of braces.
I think you shou‍ld go one further: add a comment saying // empty statement or similar.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!