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

Powershell stuck in infinite loop

powershell infinite-loop

I have powershell code that takes an array, goes through each element and updates an SQL database on another server.

delayed_job queue not being processed on Heroku

ruby-on-rails ruby-on-rails-3 heroku delayed-job

I'm running a Rails 3 app with delayed_job. The issue I've come across is that though the app is correctly adding jobs to the queue, they are never being processed.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.