• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Collection looping values outside of loop  RSS feed

 
Gary Boyer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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,
 
Junilu Lacar
Sheriff
Posts: 11434
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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).
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Boyer wrote: Thanks for the reply!

You are welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!