• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Using Collection looping values outside of loop

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the "olden days" when we looped across Vectors, we did something like this: (this one happens to be in reverse)

When we were out of the loop, since versionInfo was defined above the loop, the value in the variable was available after the loop ended.

Now, we have the new collection iteration contruct:

Is there any way to have the variable versionInfo available after the loop is over? We have old code where there is a break in the processing and the value held in the variable where it was broken is returned after the loop. I realize I can define a variable above the loop and assign it before the break, but is there some other way to do this?

And I know I should be using collections instead of Vectors, but that's another issue.

Thanks,
 
Sheriff
Posts: 17125
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only way I can think of is not going to be much of an improvement. The for-each loop construct is not meant as a wholesale replacement for the old for-loop construct. There are appropriate use cases for each and it seems like yours isn't appropriate for the for-each so why force it?

If you're curious, the solution I had in mind was to declare a variable outside the for-each loop and then assign the for-each variable to it inside the loop, when appropriate. When you exit the for-each loop, the other variable will still hold a useable reference to one of the elements in the Vector (yuck).
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So you concern about versionInfo after the loop, and after the loop it holds elementAt(1), so what is the significance of loop?
 
Gary Boyer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:The only way I can think of is not going to be much of an improvement. The for-each loop construct is not meant as a wholesale replacement for the old for-loop construct. There are appropriate use cases for each and it seems like yours isn't appropriate for the for-each so why force it?

If you're curious, the solution I had in mind was to declare a variable outside the for-each loop and then assign the for-each variable to it inside the loop, when appropriate. When you exit the for-each loop, the other variable will still hold a useable reference to one of the elements in the Vector (yuck).



Thanks, that's what I'm currently doing. I just find the for-each loop much cleaner and succinct from a code read and maintenance perspective, but you're right, I shouldn't force it. When I need the loop variable for some reason, I still use the for-loop.
 
Gary Boyer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:
So you concern about versionInfo after the loop, and after the loop it holds elementAt(1), so what is the significance of loop?



That's not the full code, I just wanted to illustrate how we are currently handling the loop. Within that, we are checking multiple values against the current versionInfo, and when one of them is found, we break. And I know there are many different ways of handling that, but my job at the moment is more cleanup than refactor. Thanks for the reply!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gary Boyer wrote: Thanks for the reply!


You are welcome
 
It's feeding time! Give me the food you were going to give to this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic