Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Array Object Chuen-Liang Chen Department of Computer Science

Similar presentations


Presentation on theme: "Java Array Object Chuen-Liang Chen Department of Computer Science"— Presentation transcript:

1 Java Array Object Chuen-Liang Chen Department of Computer Science
& Information Engineering National Taiwan University

2 Fortran Array & C Array Fortran – a contiguous space column major
declaration & allocation – INTEGER a(2,3) memory a(i, j) is located at – S + (j -1) * 2 + (i - 1) C, C++ – a contiguous space row major declaration & allocation – int a[2][3]; memory a[i][j] is located at – S + i * 3 + j a(1,1) a(2,1) a(1,2) a(2,2) a(1,3) a(2,3) S a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2] S

3 Java Array an object; a special instance of subclass of java.lang.Object an array of 3 int’s an array of 3 shorts an array of 3 (array of int)’s an array of 3 (array of short)’s int[ ] length 3 ?[0] ?[1] ?[2] int[ ][ ] length 3 ?[0] ?[1] ?[2] short[ ] length 3 ?[0] ?[1] ?[2] short[ ][ ] length 3 ?[0] ?[1] ?[2]

4 Array Variable Declaration
class variable declaration class X { int i; short j; } X x1; 32 bits a pointer; a reference to an object of X, to an object of subclass of X array variable declaration int[ ] a; OR int a[ ]; 32 bits a pointer; a reference to an arbitrary length array of int note: both of int[ ] and int[ ][ ] are direct subclasses of java.lang.Object int[ ][ ] is not the subclass of int[ ], and vise versa

5 Array Creation Expression
ordinary instance creation class X { int i; short j; } x1 = new X(); array instance creation int[ ] a; a = new int[3]; x1 X i j int[ ] length 3 ?[0] ?[1] ?[2] a

6 Array Access Expression
access of instance variable instance variables x1.i, x1.j initial value = 0 access of array element instance variables a.length (read only) initial value = 3 a[0], a[1], a[2] initial value = 0 x1 X i j int[ ] length 3 ?[0] ?[1] ?[2] a

7 Operation of Array Variable
assignment of class variable x1.i = 10; X x2 = x1; // now, x2.i == 10 assignment of array variable a[0] = 10; int[ ] b = a; // now, b[0] == 10 x1 X i 10 j x2 int[ ] length 3 ?[0] 10 ?[1] ?[2] a b

8 Array Initializer int[ ] a = {10, 20, 30}; 4 steps
array variable declaration empty array instance creation (length = 3) array elements initialization assignment of array variable

9 Array of Object (1/3) class Y { int i; Y(int p) { i = p;} void m() { … } } class Z extends Y { int j; Z(int p, int q) { super(p); j = q;} } Y[ ] c; declare an array variable, named c 32 bits; a pointer, a reference to an arbitrary length array element type: Y c

10 Array of Object (2/3) c = new Y[3]; create a 3 elements array instance
each element 32 bits; a pointer, a reference to an object of Y, or an object of subclass of Y initial value = null Y[ ] length 3 ?[0] null ?[1] ?[2] c

11 Array of Object (3/3) c[0] = new Y(10); c[1] = new Z(20, 30);
create a Y instance and a Z instance c[1].i c[1].m() access of field and method precedence of field selection, array access, method invocation are the same (15L) Y[ ] length 3 ?[0] ?[1] ?[2] null c Y i 10 Z i 20 j 30

12 Array of Array (1/3) int[ ][ ] d; declare an array variable, named d
32 bits; a pointer, a reference to an arbitrary length array element type: int[ ] Y[ ][ ] e; declare an array variable, named e element type: Y[ ] d e

13 Array of Array (2/3) d = new int[2][1]; d[1][0] = 10;
d = new int[2][ ]; d[1] = new int[2]; int[ ][ ] length 2 ?[0] ?[1] d int[ ] 1 10 int[ ][ ] length 2 ?[0] null ?[1] d int[ ]

14 Array of Array (3/3) e = new Y[2][1]; e[1][0] = new Z(20,30);
e = new Y[2][ ]; e[1] = new Y[2]; Y[ ][ ] length 2 ?[0] ?[1] e Y[ ] 1 null Z i 20 j 30 Y[ ][ ] length 2 ?[0] null ?[1] e Y[ ]


Download ppt "Java Array Object Chuen-Liang Chen Department of Computer Science"

Similar presentations


Ads by Google