<?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>Coder-Blog &#187; for-schleife</title>
	<atom:link href="http://coder-blog.de/tag/for-schleife/feed" rel="self" type="application/rss+xml" />
	<link>http://coder-blog.de</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Fri, 15 Jul 2011 17:53:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Java-Tutorial: CowSay (Kuh lernt sprechen)</title>
		<link>http://coder-blog.de/java-tutorial-cowsay-kuh-lernt-sprechen</link>
		<comments>http://coder-blog.de/java-tutorial-cowsay-kuh-lernt-sprechen#comments</comments>
		<pubDate>Thu, 09 Apr 2009 20:29:27 +0000</pubDate>
		<dc:creator>Eugen</dc:creator>
				<category><![CDATA[Java-Tutorials]]></category>
		<category><![CDATA[bedingte-anweisungen]]></category>
		<category><![CDATA[coder-blog.de]]></category>
		<category><![CDATA[cowsay]]></category>
		<category><![CDATA[for-schleife]]></category>
		<category><![CDATA[java-lernen]]></category>
		<category><![CDATA[java-tutorial]]></category>
		<category><![CDATA[objektorientierte programmierung]]></category>

		<guid isPermaLink="false">http://coder-blog.de/?p=78</guid>
		<description><![CDATA[Dieses Java-Tutorial beschreibt wie wir unser Java cowsay mit mehr Funktionalität ausstatten, sodass unsere Kuh nun sprechen lernt <a href="http://coder-blog.de/java-tutorial-cowsay-kuh-lernt-sprechen">mehr <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Lange versprochen wenden wir uns jetzt unserer Kuh aus dem zweiten Java-Tutorial zu. Schauen wir uns noch einmal an wie das Unix-Programm cowsay funktioniert.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">cowsay <span style="color: #ff0000;">'Java rocks!'</span></pre></div></div>

<p>Die Ausgabe dazu sollte so aussehen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"> _____________
<span style="color: #000000; font-weight: bold;">&lt;</span> Java rocks<span style="color: #000000; font-weight: bold;">!</span> <span style="color: #000000; font-weight: bold;">&gt;</span>
 <span style="color: #660033;">-------------</span>
           ^__^
           <span style="color: #7a0874; font-weight: bold;">&#40;</span>oo<span style="color: #7a0874; font-weight: bold;">&#41;</span>_______
            <span style="color: #7a0874; font-weight: bold;">&#40;</span>__<span style="color: #7a0874; font-weight: bold;">&#41;</span>       <span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>
                <span style="color: #000000; font-weight: bold;">||</span>----w <span style="color: #000000; font-weight: bold;">|</span>
                <span style="color: #000000; font-weight: bold;">||</span>     <span style="color: #000000; font-weight: bold;">||</span></pre></div></div>

<p>Diese Funktionalität möchten wir nun nachbauen, dass wir unser cowsay mit diesem Befehl ausführen können und das Ergebnis genauso aussehen soll wie das original cowsay aus Unix.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">java cowsay <span style="color: #ff0000;">'Java rocks!'</span></pre></div></div>

<p>Der Vorteil dabei ist jedoch, dass wir dieses Programm auf Windows, Linux, Mac OSX und allen anderen Betriebssystemen nutzen können, die auch von dem Java Runtime Environment unterstützt werden.<span id="more-78"></span><br />
Okay, fangen wir dann mal an. Zunächst erstellen wir uns eine Klasse cowsay, die auch die main-Methode enthält:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> cowsay
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Zunächst sollten wir klären wie wir es schaffen, dass der Text, den wir beim Aufruf des cowsay-Programms mitgeben auch in unseren Java-Code kommt. Dazu ist dieses String[] args da! Wie wir in den letzten Tutorials gelernt haben, handelt es sich hierbei um ein Array des Typs String. Darin finden wir die Argumente, die wir an unser Programm in der Konsole übergeben haben. Bei unserem cowsay brauchen wir lediglich auf args[0] zuzugreifen, denn wir übergeben nur ein Argument. Sollten wir jedoch mehrere Argumente übergeben wollen, so trennen wir diese durch ein Leerzeichen im Programmaufruf und finden diese in args[0] … args[n] wieder. Je nachdem wie viele davon wir übergeben hatten.<br />
Ein Problem auf das wir stoßen werden, ist dass wir die Fehlermeldung &#8220;ArrayIndexOutOfBoundException&#8221; erhalten, wenn wir beim Programmaufruf keine Nachricht übergeben. Dies werden wir mit einer bedingten Anweisung lösen und eine Standardnachricht angeben.<br />
Deshalb waren mir auch die letzten Tutorials so wichtig, bevor wir dieses hier bearbeiten konnten.<br />
Bauen wir uns dazu nun folgenden Code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> cowsay
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>args.<span style="color: #006633;">length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      args <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Ich sage nix&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">//Hier geht es noch weiter</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Hier prüfen wir mit args.length == 0, ob das args Array einen Wert zugewiesen bekommen hat. Ist dies der Fall, so ist die Länge des Arrays größer 0! Sollte es nun so sein, dass keine Nachricht eingegeben wurde, definieren wir args als ein String-Array-Objekt mit einer Länge von eins. Nun speichern wir die Standardnachricht &#8220;Ich sage nix&#8221; in das Array an der Stelle 0.</p>
<p>Wenden wir uns jetzt der Sprechblase zu. Wir müssen dabei darauf achten, dass die Größe dieser abhängig von der Länge der eingegeben Nachricht ist. Dieses Problem werden wir mit einer Schleife lösen können. Dies zeigt uns das folgende Code-Fragment:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> cowsay
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>args.<span style="color: #006633;">length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      args <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Ich sage nix&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #003399;">String</span> message <span style="color: #339933;">=</span> args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">int</span> messageLength <span style="color: #339933;">=</span> message.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">//Laenge der Nachricht</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/*********************************************
    *           Sprechblase erzeugen
    *********************************************/</span>
    <span style="color: #003399;">String</span> top <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">;</span>                <span style="color: #666666; font-style: italic;">//Obere Linie der Sprechblase</span>
    <span style="color: #003399;">String</span> bottom <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">;</span>             <span style="color: #666666; font-style: italic;">//Untere Linie der Sprechblase</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Linke und rechte Grenze mit Nachricht der Sprechblase</span>
    <span style="color: #003399;">String</span> contentAndBorders <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&amp;lt; &quot;</span> <span style="color: #339933;">+</span> message <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; &amp;gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Gesamte Sprechblase</span>
    <span style="color: #003399;">String</span> speechBubble<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Obere + untere Linie erzeugen</span>
    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> messageLength <span style="color: #339933;">+</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      top <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;_&quot;</span><span style="color: #339933;">;</span>
      bottom <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;-&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    speechBubble <span style="color: #339933;">=</span> top <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">;</span>
    speechBubble <span style="color: #339933;">+=</span> contentAndBorders <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">;</span>
    speechBubble <span style="color: #339933;">+=</span> bottom <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Jetzt folgt noch die Kuh</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Die ersten Zeilen sind die gleichen wie im obigen Code. Wir weisen der Variablen message den Inhalt des Arrays an der Stelle 0 zu, damit wir uns das Schreiben der eckigen Klammern sparen können und diese Variable einfach sprechender ist, als ein args[0].<br />
Die Variable messageLength bekommt die Länge unserer Nachricht zugewiesen, da wir diese für die Schleife brauchen werden. Als nächstes erzeugen wir uns die Variablen top und bottom, die die Zeichen für die obere bzw. untere Linie enthalten werden. Diese bekommen jeweils zunächst ein Leerzeichen zugewiesen. Damit stellen wir sicher, dass über / unter der öffnenden spitzen Klammer keine Striche stehen werden.<br />
Die Variable messageAndBorders enthält die Nachricht und den linken und rechten Begrenzer gefolgt von einem Leerzeichen, damit links und rechts von der Nachricht etwas Platz bleibt. &#8211; Sieht einfach besser aus. Dadurch müssen wir aber auch die Schleife um zwei Durchläufe erweitern, um dies auszugleichen. In dieser Schleife werden die Variablen top und bottom mit den entsprechenden Zeichen &#8220;_&#8221; und &#8220;-&#8221; gefüllt. Es werden so viele Zeichen erstellt wie die Nachricht – verlängert um zwei – lang ist.<br />
Die Variable speechBubble soll die gesamte Sprechblase enthalten und bekommt deshalb top, contentAndBorders und bottom – jeweils gefolgt von einer &#8220;newline&#8221; – zugewiesen.</p>
<p>Jetzt fehlt uns nur noch die Kuh. Eigentlich könnten wir einfach den Code aus dem CowSay-Tutorial verwenden, denn es ändert sich hierbei nichts. Um aber so viele System.out.println zu vermeiden, speichern wir die Kuh in die kuh Variable und erstellen uns eine Variable cowsay, die die Sprechblase und die Kuh enthält, die wir dann schließlich ausgeben werden.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> cowsay
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>args.<span style="color: #006633;">length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      args <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Ich sage nix&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #003399;">String</span> message <span style="color: #339933;">=</span> args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">int</span> messageLength <span style="color: #339933;">=</span> message.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">//Laenge der Nachricht</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/*********************************************
    *           Sprechblase erzeugen
    **********************************************/</span>
    <span style="color: #003399;">String</span> top <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">;</span>                <span style="color: #666666; font-style: italic;">//Obere Linie der Sprechblase</span>
    <span style="color: #003399;">String</span> bottom <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">;</span>             <span style="color: #666666; font-style: italic;">//Untere Linie der Sprechblase</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Linke und rechte Grenze mit Nachricht der Sprechblase</span>
    <span style="color: #003399;">String</span> contentAndBorders <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&amp;lt; &quot;</span> <span style="color: #339933;">+</span> message <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; &amp;gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Gesamte Sprechblase</span>
    <span style="color: #003399;">String</span> speechBubble<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Die Kuh</span>
    <span style="color: #003399;">String</span> kuh<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Die gesamte Ausgabe</span>
    <span style="color: #003399;">String</span> cowsay<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Obere + untere Linie erzeugen</span>
    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> messageLength <span style="color: #339933;">+</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      top <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;_&quot;</span><span style="color: #339933;">;</span>
      bottom <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;-&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    speechBubble <span style="color: #339933;">=</span> top <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">;</span>
    speechBubble <span style="color: #339933;">+=</span> contentAndBorders <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">;</span>
    speechBubble <span style="color: #339933;">+=</span> bottom <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Die Kuh erzeugen</span>
    kuh <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;          ^__^&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">;</span>
    kuh <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;          (oo)_______&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">;</span>
    kuh <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;           (__)       )/&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">;</span>
    kuh <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;               ||----w |&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">;</span>
    kuh <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;               ||     ||&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    cowsay <span style="color: #339933;">=</span> speechBubble <span style="color: #339933;">+</span> kuh<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>cowsay<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Jetzt ist unsere Kuh eigentlich fertig und kann auch verwendet werden. Wir werden sie aber noch in späteren Tutorials umbauen. Freut euch schon mal drauf.</p>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-cowsay-kuh-lernt-sprechen/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java-Tutorial: Arrays</title>
		<link>http://coder-blog.de/java-tutorial-arrays</link>
		<comments>http://coder-blog.de/java-tutorial-arrays#comments</comments>
		<pubDate>Tue, 07 Apr 2009 14:29:40 +0000</pubDate>
		<dc:creator>Eugen</dc:creator>
				<category><![CDATA[Java-Tutorials]]></category>
		<category><![CDATA[coder-blog.de]]></category>
		<category><![CDATA[erweiterte-for-Schleife]]></category>
		<category><![CDATA[for-schleife]]></category>
		<category><![CDATA[java-arrays]]></category>
		<category><![CDATA[java-lernen]]></category>
		<category><![CDATA[java-tutorial]]></category>
		<category><![CDATA[uni-informatik]]></category>

		<guid isPermaLink="false">http://coder-blog.de/?p=133</guid>
		<description><![CDATA[Dieses Tutorial zeigt uns die Verwendung von Arrays in Java <a href="http://coder-blog.de/java-tutorial-arrays">mehr <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Wenden wir uns in diesem Tutorial einmal den Arrays in Java zu. Ein Array – auch Feld und Reihung genannt &#8211;  kann man mit einem Setzkasten vergleichen, dessen Plätze von 0 bis 1 durchnummeriert sind. In diese Plätze können Werte gespeichert werden ähnlich wie bei einer Variablen. Ein Array enthält jedoch mehrere Werte, die über einen ganzzahligen Index angesprochen werden können. Bei späteren Berechnungen sollte man daran denken, dass ein Array mit dem Index 0 beginnt.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> zahlArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
zahlArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
zahlArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
zahlArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
zahlArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span></pre></div></div>

<p>Mit den eckigen Klammern nach dem Typ sagen wir, dass wir ein Array erzeugen möchten. Die eckigen Klammern hinter die Bezeichnung des Arrays zu setzen ist auch möglich. Mit dem Schlüsselwort &#8220;new&#8221; erzeugen wir ein neues Array-Objekt, das Integer-Werte enthält und 4 Felder groß ist. Dabei können in einem Array nur Werte gespeichert werden, die auch zu dem angegebenen Typ passen. Würden wir nun versuchen einen String in unser Array zu speichern, bekämen wir eine Fehlermeldung.<span id="more-133"></span><br />
Java bietet uns noch eine andere Möglichkeit Arrays zu erstellen:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> zahlArray<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Damit haben wir den obigen Code auf eine Zeile reduziert. Beides kann jedoch nicht kombiniert werden, sodass etwas wie dies nicht möglich ist und uns unweigerlich einen Compilerfehler bringt:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> zahlArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
zahlArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Dies können wir jedoch mit einem kleinen Trick umgehen:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> zahlArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> zahlen <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
zahlArray <span style="color: #339933;">=</span> zahlen<span style="color: #339933;">;</span></pre></div></div>

<p>Sowohl ist dies auch möglich:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> zahlArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Das größte Speichern von Arrays bringt uns nichts, wenn wir nicht auch auf die Werte darin zugreifen können. Dazu schreiben wir einfach hinter den Namen des Arrays in eckige Klammern den Indexwert, in denen unser gewünschter Wert gespeichert worden ist.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> zahlArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>zahlArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Mit zahlArray[0] greifen wir auf den Wert im Array zu, der unter dem Index 0 abgelegt worden ist. Da die Indizes bei Arrays mit 0 beginnen, erhalten wir als Ausgabe unsere 5.<br />
Einige werden sich auch schon sicher Gedanken dazu machen wie man ein ganzes Array durchlaufen könnte, um auf alle Werte zugreifen zu können. Mit Array.length können wir bequem auf die Länge unseres Arrays zugreifen und somit mit Hilfe einer Schleife – zum Beispiel – alle Elemente ausgeben.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> zahlArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> zahlArray.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>zahlArray<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Hierbei erzeugen wir wie in den obigen Beispielen das Array zahlArray und befüllen es mit den entsprechenden Werten. Mit der for-Schleife ist es uns nun möglich auf die Indizes von 0 bis 3 (zahlArray.length – 1) zuzugreifen.<br />
Versuchen wir auf ein Element zuzugreifen, dass außerhalb des Arrays liegt – also wenn wir bei uns auf zahlArray[4] zugreifen würden – bekommen wir eine ArrayIndexOutOfBoundsException Fehlermeldung.</p>
<p>Wie in anderen Programmiersprachen ist es uns in Java auch möglich mehrdimensionale Arrays zu erstellen. Mehrdimensionale Arrays kann man sich als Arrays von Arrays vorstellen. Somit könnten wir beispielsweise Koordinaten speichern und auf diese zugreifen, oder uns eine Matrix simulieren.<br />
Dies erfolgt folgendermaßen:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> matrixEins <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Alternativen</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> matrixZwei<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> matrixDrei<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Mit diesem Code haben wir uns so gesehen drei Matrizen erzeugt, die jeweils aus zwei Zeilen und drei Spalten bestehen. Nun kommen wir zur Verwendung eines dieser mehrdimensionalen Arrays:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> matrixEins <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> matrixEins.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> row <span style="color: #339933;">=</span> matrixEins<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> row.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>row<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Zunächst erzeugen wir uns wie schon weiter oben beschrieben ein zweidimensionales Array und befüllen es mit den Werten 1 und 2. Danach durchlaufen wir dieses Array mit zwei ineinander geschachtelten for-Schleifen, um auch alle Elemente zu erwischen. Dazu müssen wir uns ein neues Array anlegen, dass die jeweilige Zeile enthält und durchlaufen dieses neue Array mit der zweiten for-Schleife, um uns die Werte auszugeben, die in der aktuellen Zeile stecken. Ist diese Schleife abgearbeitet, wird dem row-Array die zweite Zeile des matrixEins-Arrays zugewiesen und die innere Schleife wird noch einmal abgearbeitet. Als Ausgabe wollten wir etwas sehen wie:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">111</span>
<span style="color: #000000;">222</span></pre></div></div>

<p>Ein letztes Thema in diesem Tutorial ist die erweiterte for-Schleife, die von den Java-Entwicklern erstellt worden ist, um uns ein wenig Tipparbeit abzunehmen.<br />
Mit der erweiterten for-Schleife könnten wir unseren obigen Code in das umwandeln:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> matrixEins <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> row <span style="color: #339933;">:</span> matrixEins<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> value <span style="color: #339933;">:</span> row<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Vom Prinzip her funktioniert die erweiterte for-Schleife wie die normale for-Schleife, unterscheidet sich jedoch in der Syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> Typ Bezeichner <span style="color: #339933;">:</span> <span style="color: #003399;">Array</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
…
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Die erweiterte for-Schleife wird solange durchlaufen, bis das ganze Array abgearbeitet ist. Dabei wird der Variablen vor dem Doppelpunkt immer der aktuelle Wert aus dem Array übergeben, den wir zum Beispiel ausgeben können. Ist der Code im Rumpf abgearbeitet, wird der Variablen der nächste Wert, der im Array steckt übergeben, der dann wieder verarbeitet werden kann.</p>
<p>Somit hätten wir das Kapitel der Arrays in Java bearbeitet und können nun unsere Kuh vom Anfang endlich zum sprechen bringen. Dies jedoch im nächsten Tutorium.</p>
<p>Ansonsten bei Fragen fragen.</p>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-arrays/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java-Tutorial: Schleifen</title>
		<link>http://coder-blog.de/java-tutorial-schleifen</link>
		<comments>http://coder-blog.de/java-tutorial-schleifen#comments</comments>
		<pubDate>Sun, 05 Apr 2009 19:57:35 +0000</pubDate>
		<dc:creator>Eugen</dc:creator>
				<category><![CDATA[Java-Tutorials]]></category>
		<category><![CDATA[coder-blog.de]]></category>
		<category><![CDATA[do-while-schleife]]></category>
		<category><![CDATA[for-schleife]]></category>
		<category><![CDATA[java-lernen]]></category>
		<category><![CDATA[java-tutorial]]></category>
		<category><![CDATA[javakurs]]></category>
		<category><![CDATA[schleifen]]></category>
		<category><![CDATA[while-schleife]]></category>

		<guid isPermaLink="false">http://coder-blog.de/?p=129</guid>
		<description><![CDATA[Dieses Java-Tutorial zeigt uns wie wir Schleifen dazu benutzen, um Code mehrmals auszufüheren. <a href="http://coder-blog.de/java-tutorial-schleifen">mehr <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In diesem Tutorial werden wir uns den Schleifen zuwenden. Um zu verdeutlichen was Schleifen sind, schauen wir und zunächst einmal diesen Code an:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//Ausgabe: 0</span>
i<span style="color: #339933;">++;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//Ausgabe: 1</span>
i<span style="color: #339933;">++;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//Ausgabe: 2</span>
i<span style="color: #339933;">++;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//Ausgabe: 3</span></pre></div></div>

<p>Wie wir sehen, erzeugen wir uns zunächst eine Variable, die einen Wert von 0 zugewiesen bekommt und dann der Wert der Variablen ausgegeben wird. Danach wird der Wert der Variablen um eins erhöht und es erfolgt wieder eine Ausgabe … usw. Nun, da Programmierer schreibfaul sind und dieser Code einfach zu umständlich erscheint, gibt es in vielen Programmiersprachen Schleifen, die dazu dienen einen bestimmten Code mehrmals auszuführen. Programmiersprachen, die keine Schleifen unterstützen, verwenden oft das Konzept der Rekursion &#8211; dies unterstützt Java auch.</p>
<p>Java kennt hierbei drei Arten von Schleifen:</p>
<ul>
<li><strong>while-Schleife</strong></li>
<li><strong> do-while-Schleife</strong></li>
<li><strong> for-Schleife</strong></li>
</ul>
<p>Schleifen bestehen immer aus einer Schleifenbedingung und dem Rumpf. In der Schleifenbedingung befindet sich ein boolescher Ausdruck, der der Schleife sagt wie lange der im Rumpf stehende Programmcode wiederholt werden soll.<span id="more-129"></span></p>
<p>Wenden wir uns zunächst der while-Schleife zu und betrachten dazu den oben abgewandelten Code in &#8220;Schleifen-Schreibweise&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  i<span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Compilieren wir nun dieses Code-Fragment, so sollten wir das gleiche Ergebnis wie oben erhalten. Doch was passiert nun hier? Zunächst erstellen wir wie oben die Variable i und weisen ihr den Wert 0 zu. Danach sehen wir die while-Schleife in Gebrauch. In den runden Klammern finden wir die Bedingung, die der Schleife sagt, wie lange sie wiederholt werden soll – nämlich so lange wie unser i kleiner-gleich drei ist. Nun gelangen wir in den Rumpf der Schleife. Wie wir sehen findet dort lediglich die Ausgabe von i statt. Danach wird i um eins erhöht. Das Erhöhen der Variablen dürfen wir nicht vergessen, denn sonst bekämen wir eine Endlosschleife, die theoretisch unendlich lang ausgeführt wird. Naja, Theorie und Praxis sehen hierbei anders aus, denn entweder erkennt unser Betriebssystem, dass es sich hier um Code handelt, der nicht vorwärts kommt und bricht diesen ab, oder wir brechen ihn selber ab, weil es uns zu doof ist so lange zu warten, oder uns schmiert irgendwann das System ab und wir müssen den Rechner neu starten. <img src='http://coder-blog.de/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> <br />
Schauen wir uns nun den obigen Code umgewandelt in eine for-Schleife an:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Was macht nun dieses Stück Code? In der for-Schleife deklarieren wir die Variable in den runden Klammern und weisen ihr den Wert 0 zu. Nach dem Semikolon folgt dann die Bedingung, die die gleiche ist wie bei der while-Schleife. Ein Semikolon weiter schreiben wir das rein, was mit unserer Zählvariablen i gemacht werden soll – sie wird um eins erhöht.<br />
Die Syntax einer for-Schleife sieht folgendermaßen aus:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>Zählvariable<span style="color: #339933;">;</span> Bedingung<span style="color: #339933;">;</span> Erhöhung der Zählvariablen<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
Code im Rumpf
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Der Code im Rumpf der Schleife wird so lange wiederholt, bis i den Wert von drei übersteigt.</p>
<p>Jetzt tun uns noch die do-while-Schleife und haben dieses Kapitel auch hinter uns <img src='http://coder-blog.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> <br />
Zunächst einmal wenden wir uns wieder einem Code-Beispiel zu:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  i<span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Okay, die do-while-Schleife unterscheidet sich von der while-Schleife einmal, dass da das &#8220;do&#8221; vor dem &#8220;while&#8221; steht und dadurch, dass eine do-while-Schleife mindestens einmal ausgeführt wird. Haben wir also eine Bedingung, die von vornherein &#8220;false&#8221; ist, so findet bei der while- und der for-Schleife keine Ausführung des Codes im Rumpf statt &#8211; jedoch bei der do-while-Schleife.</p>
<p>Nun sollte sich die Frage stellen wann man welche Schleife verwenden sollte. Bei der do-while-Schleife sollte es klar sein, dass man diese nutzt, um Code mindestens einmal ausgeführt zu haben. Bei der for- und while-Schleife ist es Wurschd, denn vom Aufwand her sollten beide gleich sein.</p>
<p>Also, noch ein fröhliches Coden … =)</p>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-schleifen/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

