
m4_changequote(,)

This tutorial teaches you how to create single-dimensional and
multi-dimensional arrays of TT(Object)s. The TT(Object) types are all
types execept for boolean, char, int, float and double. A helpful
convention in Java is that the TT(Object) types start with an uppercase
letter, while non-TT(Object) types start with a lowercase letter, such as
for example the TT(String) CLASS as an example of an TT(Object) type. In
addition to this, two different array initialization syntaxes are
presented.

@cindex Arrays
@cindex TT(Object) arrays
@cindex Arrays of TT(Object) type, single-dimensional
@cindex Single-dimensional arrays of TT(Object)s

SECT(Single-dimensional arrays of TT(Object)s)

BO(Question 4.10.1:) Here is an example of a convenient one-dimensional
array initialization syntax.  Study, compile and run the following
code.  The code TT(Person)[] should be read out loud as ``person
array'' indicating the variable TT(a) is a EM(person array), also known as
an ``array of persons''.

@example
CLASS Person
BEGIN
PD PRIVATE PROPERTY String name;

PD CONSTRUCTOR Person(String aName)
PD BEGIN
PD PD name = aName;
PD END

PD POBLIC String toString()
PD BEGIN
PD PD RETURN name;
PD END
END

CLASS PersonTest
BEGIN
PD BEGINMAIN
PD PD VAR Person[] a = @{ new Person("Person # 1"), new Person("Person # 2"), new Person("Person # 3") @};
PD PD SUPERFOR (VAR int i=0 TU a.length-1)
PD PD BEGIN
PD PD PD System.out.println("a[" + i + "]=" + a[i]);
PD PD END
PD ENDMAIN
END
@end example

@cindex Arrays of TT(Object) type, initialization
@cindex Arrays of TT(Object) type, first initialization syntax
@cindex First initialization syntax for arrays of TT(Object)s

Due to a design oversight by the creators of Java you cannot use this
syntax to re-initialize an array like so:

@example
COMM(// Compilation error)
a = @{ new Person("Person # 4"), new Person("Person # 5"), new Person("Person # 6"), new Person("Person # 7") @};
@end example

Luckily there is a way array around this oversight and that is to use
a design pattern where you introduce a temporary variable like so:

@example
COMM(// No error)
VAR Person[] temp = @{ new Person("Person # 4"), new Person("Person # 5"), new Person("Person # 6"), new Person("Person # 7") @};
a = temp; COMM(// Array "a" now holds Person # 4,Person # 5,Person # 6,Person # 7)
@end example

Later you will learn why this design pattern is useful for
re-initializing multi-dimensional arrays.

BO(Question 4.10.2:) Write a FUNCTION in the CLASS PersonTest called
TT(print) that takes a Person array argument and prints out the
array. You will need to use the length property of the array parameter
so your FUNCTION works with arbitrary sized arrays. Change the MAIN
FUNCTION to what follows so that it contains a call to the TT(print)x
FUNCTION.

@example
VAR Person[] a = @{ new Person("Person # 1"), new Person("Person # 2"), new Person("Person # 3")@};
print(a);
@end example

BO(Question 4.10.3:) Write your own class called Mine similar to the Person
class with a one int parameter constructor, a private int property p
and a toString method that converts p to a string. Then write a
FUNCTION in the PersonTest class with same name as the previous print
FUNCTION, except that this one takes a Mine[], also known as a Mine
array. You might recall from Tutorial 7 that this practice of having
two functions with the same name is called FUNCTION name
overloading. Change the MAIN FUNCTION to what follows so that it
initializes an array of Mine point variables and then calls the second
print FUNCTION.

@example
VAR Mine[] b = @{ new Mine(1), new Mine(2), new Mine(3) @};
print(b);
@end example

@cindex Arrays of TT(Object) type, second initialization syntax
@cindex Second initialization syntax for arrays of TT(Object)s

Here is an example of a second initialisation syntax. For this
particular example it is better to use the simpler, earlier
initialisation syntax, but when the size of the array to be created is
to be determined at run-time, then the second syntax should used. The
next question will show you an example of this.

@example
BEGINMAIN
PD VAR Person[] a = new Person[3];
PD COMM(// at this point the array is all nulls)
PD SUPERFOR (VAR int i=0 TU a.length-1)
PD BEGIN
PD PD a[i] = new Person("Person # " + (i+1));
PD END
PD print(a);
ENDMAIN
@end example

BO(Question 4.10.4:) Write a FUNCTION create takes one INT argument, the
size of the array to create and returns a Person array of that
size. Make it so the ith element of the array is initialised to
"Person # " + i. Call this FUNCTION from the MAIN FUNCTION like so:

@example
BEGINMAIN
PD VAR Person[] a = create(3);
PD print(a);
ENDMAIN
@end example

BO(Question 4.10.5:) Write a FUNCTION create2 takes one int argument, the
size of the array to create and returns a Mine array of that
size. Make it so the ith element of the array's toString method prints
out "Mine # " + i. Why is it not possible to overload that create
FUNCTION? Try it and see what the compiler says. Call create2 from the
MAIN FUNCTION like so:

@example
BEGINMAIN
PD VAR Mine[] a = create2(3);
PD print(a);
ENDMAIN
@end example

BO(Question 4.10.6:) Write a FUNCTION doubler that takes a Person array x
and returns a new Person array called result twice as big as x. Copy x
into the result before you return it. The extra elements in result
should all be NULL.

BO(Question 4.10.7:) Change the doubler FUNCTION so that every NULL in the
array result is set to a NEW TT(Person) make it so that every new Person
TT(Object) has a different name property.

SECT(Two-dimensional arrays of TT(Object)s)

@cindex Two-dimensional arrays of TT(Object)s
@cindex Arrays of TT(Object) type, two-dimensional
@cindex TT(Object) arrays, two-dimensional

BO(Question 4.10.8:) Here is an example of a convenient two dimensional
array initialization syntax. Study, compile and run the following
code. The code Person[][] should be read out loud as person array
array indicating the variable TT(a) is a EM(person array array), also
known as a EM(two-dimensional array of persons).

@example
BEGINMAIN
PD VAR Person[][] a = @{ @{ new Person("Person # 1"), new Person("Person # 2"), new Person("Person # 3") @},
PD PD PD PD PD PD PD @{ new Person("Person # 4"), new Person("Person # 5") @},
PD PD PD PD PD PD PD @{ new Person("Person # 6") @} @};

PD SUPERFOR (VAR int y=0 TU a.length-1)
PD BEGIN
PD PD SUPERFOR (VAR int x=0; TU a[y].length-1)
PD PD BEGIN
PD PD PD System.out.print(" " + a[y][x]);
PD PD END
PD PD System.out.println();
PD END
ENDMAIN
@end example

BO(Question 4.10.9:) By copying the pattern of the code above, do some more
overloading of the print FUNCTION by writing two new print functions,
one taking a two dimensional array of Person, the other taken a two
dimensional array of Mine. The call both of these functions from the
MAIN FUNCTION.

Since a[0] is a Person array,you would naively expect it to be able to
be re-initialised like so:

@example
a[0] = @{ new Person("Person # 4"),
PD PD PD new Person("Person # 5"),
PD PD PD new Person("Person # 6") @};
@end example

so that after this code a0 holds the four element long array TT(Person
#4),TT(Person #5) and TT(Person #6), but it does't work owing to a
design oversight by the creators of Java. Luckily as mentioned above
there is a way around this oversight and that is to use a temporary
variable like so:

@example
VAR Person[] temp = @{ new Person("Person # 4"),
PD PD PD PD PD PD PD PD new Person("Person # 5"),
PD PD PD PD PD PD PD PD new Person("Person # 6") @};
a[0] = temp; COMM(// Array "a[0]" now holds Person # 4,Person # 5,Person # 6)
@end example

Like with one-dimensional arrays, there is a second initialization
syntax for two-dimensional arrays and here it is. Unlike the above
code the sub-arrays TT(a[0]),TT(a[1]) and TT(a[2]) are all of equal
size, namely three.

@example
BEGINMAIN
PD VAR Person[][] a = new Person[3][3];
PD a[0][0] = new Person("Person # 1");
PD a[0][1] = new Person("Person # 2");
PD a[0][2] = new Person("Person # 3");
PD a[1][0] = new Person("Person # 4");
PD a[1][1] = new Person("Person # 5");
PD a[1][2] = new Person("Person # 6");
PD a[2][0] = new Person("Person # 7");
PD a[2][1] = new Person("Person # 8");
PD a[2][2] = new Person("Person # 9");
ENDMAIN
@end example

BO(Question 4.10.10:) Write a FUNCTION create3 and create4
that takes an INT argument size and returns a two-dimensional array of
TT(Person) or TT(Mine), respectively. Make is so that each TT(Person) or
TT(Mine) TT(Object) has its own number, using a separate counter
variable TT(VAR INT count).

SECT(Three-dimensional arrays of TT(Object)s)

@cindex Three-dimensional arrays of TT(Object)s
@cindex Arrays of TT(Object) type, three-dimensional

BO(Question 4.10.11:) Using the knowledge you have gained so far about
arrays, create, initialize and print a three-dimensional array of
TT(Person)s. Make it so that each TT(Person) TT(Object) is given its
own number using a separate counter variable TT(VAR INT count).

m4_changequote([,])
