Properties of a java for loop

Consider a simple java for loop:

for (int x = 0; x < bigArray.size(); x++ ) {
     Someobject object = (Someobject) bigArray.get(x);
      process(object);
 }

private void   process(Someobject object) {
  Someobject newobject = moreProcessing(object);
  bigArray.add(newobject);
 }

My observation is that the for loop will terminate even though I am adding additional objects to the end of bigArray and bigArray is constantly getting bigger. Is this because the JRE fixes the value of the testing condtion (in this case x < bigArray.size()) before the for loop begins and never updates this value?

Answers


The only problem I see with the OP is that for an empty bigArray of type List the loop will terminate since it has to call process inside the loop to add elements and increase the size hence the condition in the for loop becomes true but the size is 0 at that point. If the list is not empty then the x < bigArray.size() will always compare to the latest length i.e. including the objects added in process. I assume that bigArray is of type java.util.List


In most cases test condition will be evaluated before start of each iteration.

More here


Need Your Help

How to link scrollbar and scrollviewer

wpf scrollbar scrollviewer

I currently have two ScrollViewer's containing alternate views of the same collection. I have bound the scrolling of the two scrollviewers together by handling the ScrollChanged event and using

Django render template from media folder

django templates renderer

I'm trying to provide additional level of flexibility for some views, specifically, I want to be able to render a view using a django template from the media location (say referred to by a FileFiel...

iPhone Xcode Settings.bundle Plist Help

iphone objective-c xcode iphone-sdk-4.1 settings.bundle

I followed the tutorial: http://useyourloaf.com/blog/2010/5/18/adding-a-settings-bundle-to-an-iphone-app.html