Lernpfad:Objektorientierte Programmierung mit Java/Arrays: Unterschied zwischen den Versionen

Zur Navigation springen Zur Suche springen
keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 130: Zeile 130:
|}
|}
</lückentext>
</lückentext>
{{Aufgabe:End}}
{{Lösung:Start}}
1.
{| class="wikitable code text-center"
!Inhalt
| 0 || 0 || 0 || 0 || 0 || 0
|-
!Index
|width="30px"|0
|width="30px"|1
|width="30px"|2
|width="30px"|3
|width="30px"|4
|width="30px"|5
|}
2.
{| class="wikitable code text-center"
!Inhalt
| 0 || 0 || 0 || 0 || 4 || 0
|-
!Index
|width="30px"|0
|width="30px"|1
|width="30px"|2
|width="30px"|3
|width="30px"|4
|width="30px"|5
|}
3. Das Array hat die Größe 6 und gültige Indizes für die Elemente sind <code>0,1,2,3,4,5</code>. Die Zuweisung <code>zahlen[6] = 6</code> erzeugt einen Fehler, da <code>6</code> kein gültiger Index ist.
4.
{| class="wikitable code text-center"
!Inhalt
| 8 || 9 || 10 || 0 || 4 || 0
|-
!Index
|width="30px"|0
|width="30px"|1
|width="30px"|2
|width="30px"|3
|width="30px"|4
|width="30px"|5
|}
5.
{| class="wikitable code text-center"
!Inhalt
| 8 || 9 || 10 || 0 || 4 || 8
|-
!Index
|width="30px"|0
|width="30px"|1
|width="30px"|2
|width="30px"|3
|width="30px"|4
|width="30px"|5
|}
6.
{| class="wikitable code text-center"
!Inhalt
| 8 || 9 || 10 || 6 || 4 || 8
|-
!Index
|width="30px"|0
|width="30px"|1
|width="30px"|2
|width="30px"|3
|width="30px"|4
|width="30px"|5
|}
{{Lösung:End}}
== Über ein Array iterieren ==
Da man oft die Größe eines Arrays im Vorfeld nicht kennt, muss man häufig mit einer Zählschleife ''über das Array iterieren'', um Aktionen auf oder mit den Elementen auszuführen. [[wikipedia:Iteration#Informatik|''Iterieren'']] bedeutet hier, das Array vom ersten bis zum letzten Element zu durchlaufen. Dazu kann man jedes Schleifenkonstrukt benutzen, üblich ist aber eine Zählschleife.
<syntaxhighlight lang="java">
// Zählschleife von 0 bis zahlen.length
// Die Zählvariable i durchläuft alle Indizes des Arrays
// Das Attribut "length" eines Arrays enthält seine Größe
for( int i = 0; i < zahlen.length; i += 1 ) {
System.out.println(zahlen[i]);
}
</syntaxhighlight>
{{Aufgabe:Start}}
Notiere in der ersten Zeile jeder Tabelle die Werte, die nach Ausführung des gezeigten Quelltextes im Array gespeichert sind. Falls der Quelltext irgendwo einen Fehler enthält, dann wird der Code nicht ausgeführt (auch korrekte Zeilen nicht). Das Array ändert sich also nicht.
{| class="wikitable code text-center"
!Inhalt
| 8 || 9 || 10 || 6 || 4 || 8
|-
!Index
|width="30px"|0
|width="30px"|1
|width="30px"|2
|width="30px"|3
|width="30px"|4
|width="30px"|5
|}
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
for( int i = 0; i < zahlen.length; i++ ) {
for( int i = 0; i < zahlen.length; i++ ) {
Zeile 167: Zeile 264:
! Inhalt
! Inhalt
| '''12()''' || '''13()''' || '''14()''' ||  '''17()''' ||  '''18()''' ||  '''19()'''
| '''12()''' || '''13()''' || '''14()''' ||  '''17()''' ||  '''18()''' ||  '''19()'''
|-
! Index
| 0 || 1 || 2 || 3 || 4 || 5
|}
</lückentext>
<syntaxhighlight lang="java">
for( int i = zahlen.length; i > 0; i-- ) {
    zahlen[i-1] = zahlen[i];
}
</syntaxhighlight>
<lückentext>
{| class="wikitable text-center code"
! Inhalt
| '''12()''' || '''13()''' || '''14()''' ||  '''17()''' ||  '''18()''' ||  '''19()'''
|-
! Index
| 0 || 1 || 2 || 3 || 4 || 5
|}
</lückentext>
<syntaxhighlight lang="java">
for( int i = 1; i < zahlen.length; i += 2 ) {
    zahlen[i] = zahlen[i-1];
}
</syntaxhighlight>
<lückentext>
{| class="wikitable text-center code"
! Inhalt
| '''12()''' || '''12()''' || '''14()''' ||  '''14()''' ||  '''18()''' ||  '''18()'''
|-
! Index
| 0 || 1 || 2 || 3 || 4 || 5
|}
</lückentext>
<syntaxhighlight lang="java">
for( int i = zahlen.length-1; i > 0; i -= 1 ) {
    zahlen[i] = zahlen[i]/2;
}
</syntaxhighlight>
<lückentext>
{| class="wikitable text-center code"
! Inhalt
| '''12()''' || '''6()''' || '''7()''' ||  '''7()''' ||  '''9()''' ||  '''9()'''
|-
|-
! Index
! Index
Zeile 177: Zeile 316:
{| class="wikitable code text-center"
{| class="wikitable code text-center"
!Inhalt
!Inhalt
| 0 || 0 || 0 || 0 || 0 || 0
| 18 || 19 || 20 || 16 || 14 || 18
|-
|-
!Index
!Index
Zeile 190: Zeile 329:
{| class="wikitable code text-center"
{| class="wikitable code text-center"
!Inhalt
!Inhalt
| 0 || 0 || 0 || 0 || 4 || 0
| 10 || 11 || 12 || 13 || 14 || 15
|-
!Index
|width="30px"|0
|width="30px"|1
|width="30px"|2
|width="30px"|3
|width="30px"|4
|width="30px"|5
|}
3. Das Array hat die Größe 6 und gültige Indizes für die Elemente sind <code>0,1,2,3,4,5</code>. Die Zuweisung <code>zahlen[6] = 6</code> erzeugt einen Fehler, da <code>6</code> kein gültiger Index ist.
 
4.
{| class="wikitable code text-center"
!Inhalt
| 8 || 9 || 10 || 0 || 4 || 0
|-
|-
!Index
!Index
Zeile 215: Zeile 339:
|width="30px"|5
|width="30px"|5
|}
|}
5.
3.
{| class="wikitable code text-center"
{| class="wikitable code text-center"
!Inhalt
!Inhalt
| 8 || 9 || 10 || 0 || 4 || 8
| 12 || 13 || 14 || 17 || 18 || 19
|-
|-
!Index
!Index
Zeile 228: Zeile 352:
|width="30px"|5
|width="30px"|5
|}
|}
6.
4.
{| class="wikitable code text-center"
{| class="wikitable code text-center"
!Inhalt
!Inhalt
| 8 || 9 || 10 || 6 || 4 || 8
| 12 || 13 || 14 || 17 || 18 || 19
|-
|-
!Index
!Index
Zeile 241: Zeile 365:
|width="30px"|5
|width="30px"|5
|}
|}
7.
5.
{| class="wikitable code text-center"
{| class="wikitable code text-center"
!Inhalt
!Inhalt
| 18 || 19 || 20 || 16 || 14 || 18
| 12 || 12 || 14 || 14 || 18 || 18
|-
|-
!Index
!Index
Zeile 254: Zeile 378:
|width="30px"|5
|width="30px"|5
|}
|}
8.
6.
{| class="wikitable code text-center"
{| class="wikitable code text-center"
!Inhalt
!Inhalt
| 10 || 11 || 12 || 13 || 14 || 15
| 12 || 6 || 7 || 7 || 9 || 9
|-
!Index
|width="30px"|0
|width="30px"|1
|width="30px"|2
|width="30px"|3
|width="30px"|4
|width="30px"|5
|}
9.
{| class="wikitable code text-center"
!Inhalt
| 12 || 13 || 14 || 17 || 18 || 19
|-
|-
!Index
!Index
Zeile 282: Zeile 393:
{{Lösung:End}}
{{Lösung:End}}


== Über ein Array iterieren ==
== Übungen ==  
Da man oft die Größe eines Arrays im Vorfeld nicht kennt, muss man häufig mit einer Zählschleife ''über das Array iterieren'', um Aktionen auf oder mit den Elementen auszuführen. [[wikipedia:Iteration#Informatik|''Iterieren'']] bedeutet hier, das Array vom ersten bis zum letzten Element zu durchlaufen. Dazu kann man jedes Schleifenkonstrukt benutzen, üblich ist aber eine Zählschleife.
<syntaxhighlight lang="java">
// Zählschleife von 0 bis zahlen.length
// Die Zählvariable i durchläuft alle Indizes des Arrays
// Das Attribut "length" eines Arrays enthält seine Größe
for( int i = 0; i < zahlen.length; i += 1 ) {
System.out.println(zahlen[i]);
}
</syntaxhighlight>


{{Aufgabe:Start}}
{{Aufgabe:Start}}

Navigationsmenü