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

keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 96: Zeile 96:
{{Aufgabe:End}}
{{Aufgabe:End}}
{{Lösung:Start}}
{{Lösung:Start}}
{| class="table-bordered"
1.
|rowspan="2" class="cell-clear"|<syntaxhighlight lang="java">
{| class="wikitable"
int[] zahlen = new int[6];
</syntaxhighlight>
!Inhalt
!Inhalt
| 0 || 0 || 0 || 0 || 0 || 0
| 0 || 0 || 0 || 0 || 0 || 0
Zeile 105: Zeile 103:
!Index
!Index
|<code>0</code> || <code>1</code> || <code>2</code> || <code>3</code>  || <code>4</code>  || <code>5</code>
|<code>0</code> || <code>1</code> || <code>2</code> || <code>3</code>  || <code>4</code>  || <code>5</code>
|}
2.
{| class="wikitable"
!Inhalt
| 0 || 0 || 0 || 0 || 4 || 0
|-
!Index
|<code>0</code> || <code>1</code> || <code>2</code> || <code>3</code>  || <code>4</code>  || <code>5</code>
|}
3. Das Array hat die Größe 6, daher sind gültige Indizes für die Elemente 0,1,2,3,4,5. Die Zuweisung <code>zahlen[6] = zahlen[6]</code> erzeugt daher einen Fehler.
4.
{| class="wikitable"
!Inhalt
| 8 || 9 || 10 || 0 || 4 || 0
|-
|-
|rowspan="2" class="cell-clear"|<syntaxhighlight lang="java">
!Index
zahlen[4] = zahlen[4];
|<code>0</code> || <code>1</code> || <code>2</code> || <code>3</code>  || <code>4</code>  || <code>5</code>
</syntaxhighlight>
|}
5.
{| class="wikitable"
!Inhalt
!Inhalt
| 0 || 0 || 0 || 0 || 4 || 0
| 8 || 9 || 10 || 0 || 4 || 8
|-
|-
!Index
!Index
|<code>0</code> || <code>1</code> || <code>2</code> || <code>3</code>  || <code>4</code>  || <code>5</code>
|<code>0</code> || <code>1</code> || <code>2</code> || <code>3</code>  || <code>4</code>  || <code>5</code>
|}
6.
{| class="wikitable"
!Inhalt
| 8 || 9 || 10 || 6 || 4 || 8
|-
|-
|rowspan="2" class="cell-clear"|<syntaxhighlight lang="java">
!Index
for( int i = 0; i < zahlen.length; i++ ) {
|<code>0</code> || <code>1</code> || <code>2</code> || <code>3</code>  || <code>4</code>  || <code>5</code>
    zahlen[i] = zahlen[2] + i;
|}
}
6.
</syntaxhighlight>
{| class="wikitable"
!Inhalt
!Inhalt
| 10 || 11 || 12 || 15 || 16 || 17
| 10 || 11 || 12 || 15 || 16 || 17