Archive for July, 2005

Java List Tip

As I mentioned before, every Java programmer must own Effective Java by Joshua Bloch. Here is one of the tips from the book. It discusses is "Minimizing the scope of local variables" (Item 29). Part of this tip, that might be of interest to developers here is: Iteration of List's In his book, Bloch discusses the use of For loops over While loops when iterating through the contents of a list. This will minimize the scope of variables assuming that the variables are not needed later in the method. For Example,
for (Iterator i = c.iterator(); i.hasNext(); )
{
doSomething(i.next() );
}

To see why this for loop preferable to the more obvious while loop, consider the following code fragment, which contains two while loops and one bug:

Iterator i = c.iterator(); while (i.hasNext() )
{
doSomething(i.next());
}
Iterator i2 = c2.iterator();
while (i.hasNext() )
{
//BUG! doSomethingElse(i2.next());
}

The second loop contains a cut and paste error: It initiates a new loop variable, i2, but uses the old one, i. This will compile with out error and also run with error. The second loop will always terminate immediately and look like c2 does not contain anything. If the same cut and paste were done with the for loop, the loop variable from the first loop would not be in scope in the second loop and it would cause a compile error:

for (Iterator i = c.iterator(); i.hasNext(); )
{
doSomething(i.next() );
……
}
//Compile-time error- the symbol i cannot be resolved
for (Iterator i2 = c2.iterator(); i.hasNext(); )
{
doSomething(i2.next() );
……
}

Here is another tip for faster access to the loop when using random access List implementations such as ArrayList and Vector: for (int i =0, n = list.size(); i )The use of the second variable is essential to the performance of the idion. Without it, the loop would have to call the size method oncer per iteration. This is very useful if you know that there may be a large amount of elements in the list and will save vital time when iterating through the list. One note that Bloch make on this is: "Using this idiom is acceptable when you are sure the list really does provide random access: otherwise it displays quadratic performance". Hope this helps when working with Lists or Arraylists. Stephen For more details on this tip see Effective Java by Joshua Bloch

No Comments

Hello Summer

Well we are definitely getting a great summer, even if it is only going to last for a few days. I heard it got close to 30 degrees in some areas.
So what has been happening to me in the last while….
My sister got married recently. It was a great day, I managed to get some fantastic photographs. The big sis and new hubby are back from the honeymoon and enjoying married life.
It looks like it is time for myself and the misses to start arranging our own special day. Tick, tock, tick, tock……
We have the reception booked but have to start arranging all the rest of the details. She is up in Co. Donegal at the moment, teaching Irish.
I actually went up last weekend by plane, thank you to all at Aer Arann for a great flight. I don’t think I would every think of driving there again. The airport is not too far from where she is teaching so happy days…

I took a few photo’s when I was up there. I was hoping to enter a photo competition in a magazine that I read but I wasn’t happy enough with the shots. I am going to put them up on the site asap.

While the better half is away, I have been busy redecorating the house. We are changing all the doors in the house and putting down wooden floors in the hall and sitting room. If that wasn’t enough, we are changing the fireplace and painting a few walls for good measure 🙂

We are actually using a new suede paint from crown. It will be interesting to see how it looks. I have not started doing the decking out the back yet, Rome wasn’t built in a day and all that.

Before I forget to mention it, I can’t get over the terrible attacks in London, last week. Thank God everyone I know over there is accounted for and safe.

I got the last results of my modules from college, I managed to get an A in the Distributed Systems module so I have a pretty good result from the modules: 2 A’s and a B+.

There was some very interesting lectures in Distributed Systems on data replication and consistency models. It should come in very handy in the research project.

No Comments