• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

IndexOF Method for LinkedStack

 
Ranch Hand
Posts: 41
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ;)

 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, the class for LinkedStack is looking like this :

 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the top is type of LinearNode class as I posted above
 
Constantin Cornea
Ranch Hand
Posts: 41
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes the T is just generic type not bounded generic type
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the Linear Node class is given by my teacher and here it can't be some mistakes

 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes sure

 
Constantin Cornea
Ranch Hand
Posts: 41
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed much simpler! Had not seen this method before, thanks for pointing out.
 
Constantin Cornea
Ranch Hand
Posts: 41
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank so much ;)
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic