<?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; java-arrays</title>
	<atom:link href="http://coder-blog.de/tag/java-arrays/feed/" rel="self" type="application/rss+xml" />
	<link>http://coder-blog.de</link>
	<description>Computer, Informatik, Internet und Programmieren</description>
	<lastBuildDate>Wed, 23 Sep 2009 09:25:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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 15: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]]></description>
			<content:encoded><![CDATA[<p>Wenden wir uns in diesem Tutorial einmal den <strong>Arrays</strong> 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.</p>
<p>Am besten verdeutlicht man sich das mit einem Bild:</p>
<div class="mceTemp">
<dl id="attachment_134" class="wp-caption alignnone" style="width: 299px;">
<dt class="wp-caption-dt"><img class="size-full wp-image-134" title="Java-Tutorial: Arrays" src="http://coder-blog.de/wp-content/uploads/2009/04/tut_array.gif" alt="Bildliche Darstellung eines Arrays" width="289" height="60" /></dt>
</dl>
</div>
<p>Hierbei werden die Werte 5, 2, 3, 4 an die Stellen von 0 bis 3 gespeichert. Bei späteren Berechnungen sollte man daran denken, dass ein Array mit dem Index 0 beginnt.</p>
<p>Überführen wir nun unser Bild in Java-Code, um die Verwendung von Arrays zu verdeutlichen:</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 <strong>Array-Objekt</strong>, 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 <strong>Compilerfehler</strong> 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 <strong>Array.length</strong> 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;">&lt;</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 <strong>for-Schleife</strong> 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 <strong>ArrayIndexOutOfBoundsException</strong> Fehlermeldung.</p>
<p>Wie in anderen Programmiersprachen ist es uns in Java auch möglich <strong>mehrdimensionale Arrays</strong> 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;">&lt;</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;">&lt;</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 <strong>zweidimensionales Array</strong> und befüllen es mit den Werten 1 und 2. Danach durchlaufen wir dieses Array mit zwei ineinander <strong>geschachtelten for-Schleifen</strong>, 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 <strong>erweiterte for-Schleife</strong>, 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 <strong>Arrays in Java</strong> 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: Fallunterscheidungen (Bedingte Anweisungen)</title>
		<link>http://coder-blog.de/java-tutorial-fallunterscheidungen-bedingte-anweisungen/</link>
		<comments>http://coder-blog.de/java-tutorial-fallunterscheidungen-bedingte-anweisungen/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 23:28:04 +0000</pubDate>
		<dc:creator>Eugen</dc:creator>
				<category><![CDATA[Java-Tutorials]]></category>
		<category><![CDATA[bedingte-anweisungen]]></category>
		<category><![CDATA[else-Anweisung]]></category>
		<category><![CDATA[else-if-anweisung]]></category>
		<category><![CDATA[fallunterscheidungen]]></category>
		<category><![CDATA[if-Anweisung]]></category>
		<category><![CDATA[java-arrays]]></category>
		<category><![CDATA[java-lernen]]></category>
		<category><![CDATA[java-tutorial]]></category>
		<category><![CDATA[objektorientierte programmierung]]></category>
		<category><![CDATA[switch-anweisung]]></category>
		<category><![CDATA[variablen]]></category>
		<category><![CDATA[verschachtelte-fallunterscheidung]]></category>

		<guid isPermaLink="false">http://coder-blog.de/?p=103</guid>
		<description><![CDATA[Dieses Java-Tutoral bringt uns Fallunterscheidungen oder auch bedingte Anweisungen näher]]></description>
			<content:encoded><![CDATA[<p>Wie in anderen Programmiersprachen, so bietet natürlich auch Java die Möglichkeit der <strong>Fallunterscheidung</strong> zu nutzen, die auch in mancher Literatur als <strong>bedingte Anweisung</strong> beschrieben wird.<br />
Ohne Fallunterscheidungen wäre ein Programm ziemlich doof, denn es könnte nicht flexibel Programmteile ausführen, die nur unter einer <strong>bestimmten Bedingung</strong> aufgerufen werden soll. Dazu bietet Java die <strong>if-</strong> bzw. <strong>if/else-Anweisungen</strong> und die <strong>switch-Anweisung</strong>.</p>
<p><strong>Bedingte Anweisungen</strong> sind gar nicht so schwer – also fangen wir gleich mit der <strong>if-Anweisung</strong> 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> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">55</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> rentenalter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">!=</span> rentenalter<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><span style="color: #0000ff;">&quot;Rentenalter nicht erreicht – weiterackern!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Was macht dieses Programm nun? Wir setzen uns zwei Variablen mit den Werten 55 und 66. Die Variable meinAlter soll das jetzige Alter repräsentieren. Die Zweite enthält das gegenwärtige Rentenalter 66. Darunter sehen wir die <strong>if-Anweisung</strong>, die – wie jede if-Anweisung – mit dem Schlüsselwort if eingeleitet wird und in Klammern einen <strong>boolschen Wert</strong> enthält.<br />
<span id="more-103"></span><br />
Hä, ich sehe aber gar keinen boolschen Wert?! Auf den ersten Blick erkennt man dies auch nicht sofort, doch der <strong>Operator !=</strong> vergleicht zwei Werte auf <strong>Nicht-Gleichheit</strong> und liefert uns einen boolschen Wert als Ergebnis. Als Gegenstück dazu gibt es den <strong>== Operator</strong>, der zwei <strong>Werte auf Gleichheit</strong> hin überprüft. In unserem Fall würde es bedeuten, dass wir den Wert der Variablen meinAlter mit dem Wert der Variablen rentenalter vergleichen. Wir fragen uns also: &#8220;Ist der Wert der Variablen meinAlter ungleich des Wertes der Variablen rentenalter?&#8221;. Als Ergebnis bekommen wir von Java ein true – also es ist wahr, dass beide ungleich sind.<br />
Da diese Bedingung nun wahr ist, wir der Code, der sich unterhalb der <strong>if-Anweisung</strong> in den geschweiften Klammern befindet ausgeführt.<br />
In normaler Sprache ausgedrückt, könnte man diese Bedingung auch schreiben als: &#8220;Wenn meinAlter ungleich rentenalter ist, dann gib mir aus &#8216;Rentenalter nicht erreicht – weiterackern!&#8217;&#8221;.</p>
<p>Natürlich ist diese Bedingung nicht gut programmiert, denn wenn meinAlter einen Wert enthält, der größer als 66 ist, wir die Meldung ebenfalls ausgegeben. Stattdessen sollten wir den <strong>< Operator</strong> benutzen, der wie in der Mathematik prüft, ob der erste Wert kleiner dem zweiten Wert 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> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">55</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> rentenalter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&lt;</span> rentenalter<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><span style="color: #0000ff;">&quot;Rentenalter nicht erreicht – weiterackern!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So ist der Code schon viel besser. Was ist, wenn wir aber auch etwas ausführen möchten, wenn die if-Anweisung nicht zutrifft? Dafür gibt es eben die<strong> if-/else-Anweisung</strong>. Fügen wir nun unserem if- einen <strong>else-Zweig</strong> hinzu.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">55</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> rentenalter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&lt;</span> rentenalter<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><span style="color: #0000ff;">&quot;Rentenalter nicht erreicht – weiterackern!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">else</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><span style="color: #0000ff;">&quot;Yay, jetzt ist erstmal ausruhen angesagt!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ins else wird immer nur dann gesprungen, wenn das if nicht zutrifft. Bei uns würde eben die if-Anweisung zuschlagen, da unsere meinAlter-Variable kleiner ist als die rentenalter.<br />
Ändern wir nun meinAlter zum Beispiel in 80, so würde die if-Anweisung nicht mehr zutreffen und dem Else-Zweig Beachtung geschenkt werden.</p>
<p>Es sind auch <strong>verschachtelte Fallunterscheidungen</strong> 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> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> rentenalter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&lt;</span> rentenalter<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><span style="color: #0000ff;">&quot;Rentenalter nicht erreicht – weiterackern!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">969</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><span style="color: #0000ff;">&quot;Na, willst du Methusalem Konkurrenz machen?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">else</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><span style="color: #0000ff;">&quot;Yay, jetzt ist erstmal ausruhen angesagt!&quot;</span><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>Wir haben meinAlter auf 1000 geändert. Da jetzt die Bedingung der <strong>if-Anweisung</strong> nicht zutrifft, wird in die <strong>else-Anweisung</strong> gesprungen. Darin befindet sich nun ebenfalls eine <strong>if-/else-Anweisung</strong>. Innerhalb des else-Zweiges, wird jetzt im if geprüft, ob meinAlter größer oder gleich 969 ist. Dies trifft nun zu und es wir die entsprechende Nachricht ausgegeben.<br />
Für solch einen Fall bietet uns Java ein besonderes Konstrukt. Wir hätten also auch schreiben können:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> rentenalter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&lt;</span> rentenalter<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><span style="color: #0000ff;">&quot;Rentenalter nicht erreicht – weiterackern!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">969</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><span style="color: #0000ff;">&quot;Na, willst du Methusalem Konkurrenz machen?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">else</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><span style="color: #0000ff;">&quot;Yay, jetzt ist erstmal ausruhen angesagt!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Die <strong>else-if-Anweisung</strong> ist eine Kombination der <strong>if –</strong> und <strong>else-Anweisung</strong>. Denn trifft nun der <strong>if-Fall</strong> bei uns nicht zu, dann wird der <strong>else-if-Fall</strong> überprüft. Trifft dieser auch nicht zu, wird <strong>else</strong> ausgeführt. Als Anmerkung sei noch gesagt, dass es uns möglich ist, so viele else-if-Fälle zu benutzen wie wir wollen.</p>
<p>Neben der <strong>if-Anweisung</strong> und der <strong>if-/else-Anweisung</strong> findet man die <strong>switch-Anweisung</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>meinAlter<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">case</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><span style="color: #0000ff;">&quot;Na, heute Windeln schon gewechselt bekommen?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">66</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><span style="color: #0000ff;">&quot;Du darfst in Rente gehen!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">969</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><span style="color: #0000ff;">&quot;Methusalem lässt grüßen&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">4723</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><span style="color: #0000ff;">&quot;Der Methuselah-Baum ist so alt wie du – Respekt!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">default</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><span style="color: #0000ff;">&quot;Ja, schönes Alter!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Der <strong>switch-Anweisung</strong> wird zunächst ein Wert übergeben. Dieser wir dann überprüft, ob er sich einem bestimmten Fall (case) zuordnen lassen kann. Ist dies nicht möglich, wird der <strong>default-Fall</strong> angewandt. Der default-Fall ist optional und kann auch weggelassen werden. Bei switch ist zu beachten, dass die <strong>break-Anweisungen</strong> gesetzt sind. Ein Break bedeutet, dass nach Aufruf der break-Anweisung der weitere Durchlauf einer Anweisung abgebrochen wird. Wird ein Break bei der switch-Anweisung vergessen, so erzeugen wir einen <strong>Fall-Through</strong>. Es wird jetzt nicht abgebrochen sobald ein Fall eingetroffen ist, sondern es werden die weiter unten stehenden Fälle auch überprüft.</p>
<p>Jetzt müssen wir nur noch etwas über <strong>Arrays</strong> erfahren und wir können unser <strong>CowSay-Programm</strong> zum &#8220;Sprechen&#8221; bringen.</p>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-fallunterscheidungen-bedingte-anweisungen/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
