• Post Reply Bookmark Topic Watch Topic
  • New Topic

"Index of" method in queue  RSS feed

 
Constantin Cornea
Ranch Hand
Posts: 40
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI ! So I am having some problems how to implement for the queue the index of method?

I've figured out in somehow, but I know that could be mistaken ...  that's why I am posting here to get from an advice how to do it?

so this is what I tried

 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming your queue is implemented using an array where all the elements move one position to the front whenever you remove an element from the queue, you just need to iterate through the array once using a simple for-loop with an index, and if the element at that position is element being searched for, return the current for-loop index.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Stephan.
Another thing: the code simply looks wrong if you use == and equals() in different loops like that. By the way, please explain what line 5 means and how many times that first loop will run.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at line 19, what will happen if element isn't found in the queue?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!