<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ticklish Techs &#187; java</title>
	<atom:link href="http://www.ticklishtechs.net/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ticklishtechs.net</link>
	<description>a mostly .NET but also some other cool techs blog</description>
	<lastBuildDate>Fri, 23 Jul 2010 06:07:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Java for .net-guys or foreach in Java</title>
		<link>http://www.ticklishtechs.net/2009/04/28/java-for-net-guys-or-foreach-in-java/</link>
		<comments>http://www.ticklishtechs.net/2009/04/28/java-for-net-guys-or-foreach-in-java/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 19:09:44 +0000</pubDate>
		<dc:creator>Benjamin Schröter</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.ticklishtechs.net/2009/04/28/java-for-net-guys-or-foreach-in-java/</guid>
		<description><![CDATA[Disclaimer: I’m absolutely not the person who can tell a lot about Java. I learned it a few years ago for university, but used it most of the time only for small example stuff. Never as deep as I’m working with .net. But maybe my newest discoveries are useful for more programmers with .net background.
I [...]]]></description>
			<content:encoded><![CDATA[<p>Disclaimer: I’m absolutely not the person who can tell a lot about Java. I learned it a few years ago for university, but used it most of the time only for small example stuff. Never as deep as I’m working with .net. But maybe my newest discoveries are useful for more programmers with .net background.</p>
<p>I started using Java 1.4 and until last week I never took a look at Java 5 or 6. The only thing I was aware of were generics in Java.</p>
<h3>iterators in Java</h3>
<p>I was always annoyed in the missing of a foreach loop in Java. There is a concept of <code>iterator</code>s in Java comparable to the .net <code>Enumerator</code>s. But to loop over a list you will find in millions of Java programs the following code:</p>
<pre class="code">Iterator&lt;Integer&gt; i = list.iterator();
<span style="color: #7f0055">while</span>(i.hasNext()) {
    Integer current = i.next();

    <span style="color: #3f7f5f">// do something with the current element
</span>}</pre>
<p>Sometimes build into a regular <code>for</code> loop, but I don’t think that is more readable or convenient:</p>
<pre class="code"><span style="color: #7f0055">for</span>(Iterator&lt;Integer&gt; i = list.iterator(); i.hasNext(); ) {
    Integer current = i.next();

    <span style="color: #3f7f5f">// just do something
</span>}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>By the way: Can anyone tell me why the method is called <code>iterator()</code> and not <code>getIterator()</code> as used everywhere else in Java?</p>
<h3>foreach in Java</h3>
<p>With Java 5 (or 1.5 – call it as you like) a foreach loop was introduced. This is about five years ago, so I’m very fast in getting news from the Java world.</p>
<pre class="code"><span style="color: #7f0055">for</span>(Integer i : list) {
    <span style="color: #3f7f5f">// just use i as the current element
</span>}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>I like it. It’s working for everything that implements <code>java.lang.Iterable</code> and for regular arrays.</p>
<p><a href="http://11011.net/software/vspaste"> </a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ticklishtechs.net/2009/04/28/java-for-net-guys-or-foreach-in-java/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
