Ludwig-Maximilians-Universit¨ at M¨ unchen WS 2014/15
Institut f¨ ur Informatik Ubungsblatt 9 ¨
Prof. Dr. R. Hennicker, A. Klarl
Ubungen zu Einf¨ ¨ uhrung in die Informatik:
Programmierung und Software-Entwicklung
Aufgabe 9-1 Erzeugen von Arrays Pr¨ asenz
Gegeben ist folgendes Programm, welches nach verschiedenen Prinzipien Arrays erzeugt. Geben Sie f¨ ur dieses Programm den Zustand des Speichers (Stack und Heap) nach den Zeilen 3, 5, 7, 9 und 15 in grafischer Repr¨ asentation an.
1
p u b l i c c l a s s C r e a t e A r r a y s {
2
p u b l i c s t a t i c void m a i n ( S t r i n g [] a r g s ) {
3
i n t [] a = new i n t [ 3 ] ;
4
f o r ( i n t i = 0; i < a . l e n g t h ; i ++)
5
a [ i ] = 2 * i ;
6
7
i n t [] b = { 1 , 3 };
8
9
i n t [] c = new i n t [ a . l e n g t h + b . l e n g t h ];
10
f o r ( i n t i = 0; i < c . l e n g t h ; i ++) {
11
i f ( i < a . l e n g t h )
12
c [ i ] = a [ i ];
13
e l s e
14
c [ i ] = b [ i - a . l e n g t h ];
15
}
16
}
17
}
Aufgabe 9-2 Erzeugen von Arrays Hausaufgabe
Gegeben ist folgendes Programm, welches nach verschiedenen Prinzipien Arrays erzeugt. Geben Sie f¨ ur dieses Programm den Zustand des Speichers (Stack und Heap) nach den Zeilen 3, 5, 10, und 12 in grafischer Repr¨ asentation an.
1
p u b l i c c l a s s C r e a t e A r r a y s 2 {
2
p u b l i c s t a t i c void m a i n ( S t r i n g [] a r g s ) {
3
char [] a = { ’ A ’ , ’ L ’ , ’ L ’ , ’ E ’ , ’ N ’ , ’ E ’ , ’ U ’ , ’ N ’ , ’ E ’ };
4
5
char [] b = new char[ a . l e n g t h /2 + 1];
6
f o r ( i n t i = 0; i < a . l e n g t h / 2 + 1 ; i ++) {
7
b [ i ] = a [ i + a . l e n g t h / 2 ] ;
8
}
9
b [3] = ’ E ’ ;
10
b [4] = ’ S ’ ;
11
12
char [] c = { ’ G ’ , ’ U ’ , ’ T ’ , ’ E ’ , ’ S ’ };
13
}
14