• 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

How to get index in new for loop

 
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any help or direction would be greatly appreciated. How can I get the index in the following type of for loop:

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The enhanced for loop is meant to iterate through the array or collection from beginning to end, so if you need to handle the array or collection in another way, I'd say you should use the old for loop.
Check out this page. Especially the paragraph at the very bottom.
[ June 26, 2007: Message edited by: Eric Daly ]
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric,

Thanks for the reply. I"m not sure if there is a way to get at the index when I'm trying to loop through an object. I tried the following but I'm getting bad results:



Meaning I have one item that should be equal to 0 but because I've got the other loop that it is within then I"m getting 7 messages instead of 1. I guess I don't know how to account for "item" in the old for loop format.

Any help or direction would be appreciated. Thanks again.
[ June 26, 2007: Message edited by: Melinda Savoy ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer depends on what stockReq is and what its API looks like. Does it have any kind of get(int) method that lets you get a single item by index?

getReqItems() presumably returns an Iterator, so calling size() on it shouldn't work. The idea is to use a single old-style loop, not a double loop as you have here, retrieving the items with some kind of get(int) method. But if stockReq doesn't have such a method -- well, then you'd be stuck. So then what you'd have to do would look something like

 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest,

Thank you so much. That was the ticket. I'm still having a hard time thinking out of the box. Though still trying. I appreciate the help of those on this list.

Thanks again!
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic