« July 25, 2007 | Main | August 31, 2007 »

August 23, 2007 Archives

August 23, 2007

QUIZ: Does this code have issues?

Over my holidays I had some time to read some books from cover to cover. Since my family requested a good amount of attention, I managed to finish only two, but this is fine as I'm in the middle of the next.

Where is the issue in this code?

There are many ways to iterate through lists in Java. One of my favorite is this one:
for (int i = 0; i < list.size(); ++i) {
  Object obj = list.get(i);
  // do something
}
I developed a habit to change it into this form:
for (int i = 0, size = list.size(); i < size; ++i) {
  Object obj = list.get(i);
  // do something
}

It is at least faster then the first version. Yes, it is. Consider a type of Vector with all methods synchronized for the list...

But, where is the issue?

Credits

This was inspired by Brian Duff's other blog.

About August 2007

This page contains all entries posted to Olaf Heimburger's Blog in August 2007. They are listed from oldest to newest.

July 25, 2007 is the previous archive.

August 31, 2007 is the next archive.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type and Oracle