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

keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 49: Zeile 49:
Zugriffe mit Indizes außerhalb des gültigen Bereichs (also <code>i < 0</code> oder <code>i > length</code>) produzieren einen [https://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html Fehler].
Zugriffe mit Indizes außerhalb des gültigen Bereichs (also <code>i < 0</code> oder <code>i > length</code>) produzieren einen [https://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html Fehler].
{{Aufgabe:Start}}
{{Aufgabe:Start}}
Notiere zu jedem Quelltext unten eine Tabelle der Form
Notiere in der ersten Zeile jeder Tabelle die Werte, die nach Ausführung des gezeigten Quelltextes im Array gespeichert sind. Falls der Quelltext einen Fehler enthält ändert sich das Array nicht.
{| class="wikitable"
!Inhalt
|width="30px"|
|width="30px"|
|width="30px"|
|width="30px"|
|width="30px"|
|width="30px"|
|-
!Index
| || || || || ||
|}
Trage jeweils den Zustand des Arrays nach der Ausführung ein. Falls der Quelltext einen Fehler enthält notiere diesen anstatt der Tabelle.


# <syntaxhighlight lang="java">
<syntaxhighlight lang="java">
int[] zahlen = new int[6];
int[] zahlen = new int[6];
</syntaxhighlight>
</syntaxhighlight>
# <syntaxhighlight lang="java">
<lückentext>
{| class="wikitable text-center code"
! Inhalt
| '''0()''' || '''0()''' || '''0()''' ||  '''0()''' ||  '''0()''' ||  '''0()'''
|-
! Index
| 0 || 1 || 2 || 3 || 4 || 5
|}
</lückentext>
<syntaxhighlight lang="java">
zahlen[4] = 4;
zahlen[4] = 4;
</syntaxhighlight>
</syntaxhighlight>
# <syntaxhighlight lang="java">
<lückentext>
{| class="wikitable text-center code"
! Inhalt
| '''0()''' || '''0()''' || '''0()''' ||  '''0()''' ||  '''4()''' ||  '''0()'''
|-
! Index
| 0 || 1 || 2 || 3 || 4 || 5
|}
</lückentext>
<syntaxhighlight lang="java">
zahlen[5] = 5;
zahlen[5] = 5;
zahlen[6] = 6;
zahlen[6] = 6;
</syntaxhighlight>
</syntaxhighlight>
# <syntaxhighlight lang="java">
<lückentext>
{| class="wikitable text-center code"
! Inhalt
| '''0()''' || '''0()''' || '''0()''' ||  '''0()''' ||  '''4()''' ||  '''0()'''
|-
! Index
| 0 || 1 || 2 || 3 || 4 || 5
|}
</lückentext>
<syntaxhighlight lang="java">
zahlen[2] = zahlen[4] + 6;
zahlen[2] = zahlen[4] + 6;
zahlen[1] = zahlen[2] - 1;
zahlen[1] = zahlen[2] - 1;
zahlen[0] = zahlen[1] - 1;
zahlen[0] = zahlen[1] - 1;
</syntaxhighlight>
</syntaxhighlight>
# <syntaxhighlight lang="java">
<lückentext>
{| class="wikitable text-center code"
! Inhalt
| '''8()''' || '''9()''' || '''10()''' ||  '''0()''' ||  '''4()''' ||  '''0()'''
|-
! Index
| 0 || 1 || 2 || 3 || 4 || 5
|}
</lückentext>
<syntaxhighlight lang="java">
if( zahlen[3] == 0 && zahlen[0] == 0 ) {
if( zahlen[3] == 0 && zahlen[0] == 0 ) {
     zahlen[5] = zahlen[2] / 2;
     zahlen[5] = zahlen[2] / 2;
Zeile 86: Zeile 109:
}
}
</syntaxhighlight>
</syntaxhighlight>
# <syntaxhighlight lang="java">
<lückentext>
{| class="wikitable text-center code"
! Inhalt
| '''8()''' || '''9()''' || '''10()''' ||  '''0()''' ||  '''4()''' ||  '''8()'''
|-
! Index
| 0 || 1 || 2 || 3 || 4 || 5
|}
</lückentext>
<syntaxhighlight lang="java">
zahlen[3] = zahlen.length;
zahlen[3] = zahlen.length;
</syntaxhighlight>
</syntaxhighlight>
# <syntaxhighlight lang="java">
<lückentext>
{| class="wikitable text-center code"
! Inhalt
| '''8()''' || '''9()''' || '''10()''' ||  '''6()''' ||  '''4()''' ||  '''8()'''
|-
! Index
| 0 || 1 || 2 || 3 || 4 || 5
|}
</lückentext>
<syntaxhighlight lang="java">
for( int i = 0; i < zahlen.length; i++ ) {
    zahlen[i] = 10 + i;
}
</syntaxhighlight>
<lückentext>
{| class="wikitable text-center code"
! Inhalt
| '''10()''' || '''11()''' || '''12()''' ||  '''13()''' ||  '''14()''' ||  '''15()'''
|-
! Index
| 0 || 1 || 2 || 3 || 4 || 5
|}
</lückentext>
<syntaxhighlight lang="java">
for( int i = 0; i < zahlen.length; i++ ) {
for( int i = 0; i < zahlen.length; i++ ) {
     zahlen[i] = zahlen[2] + i;
     zahlen[i] = zahlen[2] + i;
}
}
</syntaxhighlight>
</syntaxhighlight>
<lückentext>
{| class="wikitable text-center code"
! Inhalt
| '''12()''' || '''13()''' || '''14()''' ||  '''17()''' ||  '''18()''' ||  '''19()'''
|-
! Index
| 0 || 1 || 2 || 3 || 4 || 5
|}
</lückentext>
{{Aufgabe:End}}
{{Aufgabe:End}}
{{Lösung:Start}}
{{Lösung:Start}}
Zeile 163: Zeile 227:
|width="30px"|5
|width="30px"|5
|}
|}
6.
7.
{| class="wikitable code text-center"
!Inhalt
| 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
|}
8.
{| class="wikitable code text-center"
{| class="wikitable code text-center"
!Inhalt
!Inhalt
| 10 || 11 || 12 || 15 || 16 || 17
| 12 || 13 || 14 || 17 || 18 || 19
|-
|-
!Index
!Index