Wednesday, January 28, 2009

Iterating through a Collection in Java

There are three common ways to iterate through a Collection in Java using either while(), for() or for-each(). While each technique will produce more or less the same results, the for-each construct is the most elegant and easy to read and write. It doesn't require an Iterator and is thus more compact and probably more efficient. It is only available since Java 5 so you can't use it if you are restrained to Java 1.4 or earlier. Following, the three common methods for iterating through a Collection are presented, first using a while loop, then a for loop, and finally a for-each loop. The Collection in this example is a simple ArrayList of Strings.



While
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class WhileIteration {

public static void main(String[] args) {

Collection<String> collection = new ArrayList<String>();

collection.add("zero");
collection.add("one");
collection.add("two");

Iterator iterator = collection.iterator();

// while loop
while (iterator.hasNext()) {
System.out.println("value= " + iterator.next());
}
}
}




For
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class ForIteration {

public static void main(String[] args) {

Collection<String> collection = new ArrayList<String>();

collection.add("zero");
collection.add("one");
collection.add("two");

// for loop
for (Iterator<String> iterator = collection.iterator(); iterator.hasNext();) {
System.out.println("value= " + iterator.next());
}
}
}



For-Each
import java.util.ArrayList;
import java.util.Collection;

public class ForEachInteration {

public static void main(String[] args) {

Collection<String> collection = new ArrayList<String>();

collection.add("zero");
collection.add("one");
collection.add("two");

// for-each loop
for (String s : collection) {
System.out.println("value= " + s);
}
}
}


The result for each method should look like this:
value= zero
value= one
value= two


Friday, January 9, 2009

Ten German Birds

Moving to a new country opens up the opportunity to see and photograph many new things, in this case new birds in Germany. The following pictures were taken with a Canon 40D DSLR, with a Canon 70-200mm F4.0L IS lens and a Tamron 1.4X teleconverter. Click on the images for a higher-resolution view. You may also find interesting some photos I took of insects a while ago in Wisconsin.

Fasan


Hofente

Blaesshuhn

Teichhuhn

Amsel

Huehner

Rotkehlchen

Blaumeise

Huehner

Hahn


Greylag Goose - Graugans

Submit Photos to Shutterstock and make $$$!


Monday, January 5, 2009

Home made PBR - Photo Bio Reactor

Mmmmmmm, PBR. Either a cheap cold beer or a Photo Bio Reactor. Jared Bouck at AlgaeGeek has been experimenting with different designs and construction techniques for cultivating algae. The main reason you'd want to cultivate algae is to create a renewable fuel by taking carbon dioxide out of the atmosphere and turning it into a type of biodiesel. Millions of research dollars have been spent at Universities across the globe on discovering and genetically engineering the most ideal algae strains and building large scale biodiesel farms in warm areas. I've read somewhere that you can get around 10 times more fuel from algae than soybeans or corn given an equal cultivation area. I haven't really done too much research on cultivating algae at home and the advantage of algae, but it seems like it could be something worth pursuing. Anybody in Germany have an algae culture they want to share with me?





If you want to read more about that Algae Geek's projects check out his website. There are some funny passages there such as: "For this project we will be using some acrylic sheet that is 1/2 inch thick. This will be cut using a hole saw to make the plugs for the tube ends as well as making the holes for the plumbing. Now I realize that everything can be measured in carbon and energy required in making a product... and I realize that I will get email saying how plastics will kill the earth. Well... your likely right. So I wanted you to know preemptively that I actually got this magic carbon / pollution free acrylic from my future self coming back in time with a carbon free time machine to give it to myself so I could feel guilt free about using it. For those not fortunate enough to have a future self with a carbon free time machine and carbon free acrylic just go to your local plastics supplier. (Use your phone book, yes it’s in there)".

As a proof of concept experiment to test the feasibility of algae as a source of fuel for commercial consumption, the first 90-minute flight by a Continental Boeing 737-800 was just completed where one of the two engines was powered by a 50-50 blend of biofuel and normal aircraft fuel. No modifications of the engine were needed. The tests included an engine shutdown and restart at 38,000 feet. "Algae is viewed by many as a key fuel for the future because it is fast growing, does not compete with food crops for arable land, and yields up to 30 times more fuel than standard energy crops. But despite advances in the technology, biofuels derived from algae have yet to be proven as commercially competitive."