
m4_changequote(,)

This tutorial teaches you how to create single-dimensional and
multi-dimensional arrays of non-TT(Object)s. The non-TT(Object) types
in Java are those which aren't declared inside a class, so it includes
the following types: BOOLEAN, CHAR, INT, FLOAT and DOUBLE. A helpful
convention in Java is that the non-object types start with a lowercase
letter, while object types start with an uppercase 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 Introducing non-TT(Object) arrays
@cindex Introducing single-dimensional non-TT(Object) arrays
@cindex Arrays of non-TT(Object) type, single-dimensional
@cindex Non-TT(Object) arrays, single-dimensional

@subsection Single-dimensional non-Object arrays

@anchor{Single-dimensional non-Object arrays}

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

m4_changequote(,)

BO(Question 4.7.1:) Here is an example of a convenient one dimensional
array initialization syntax. Study, compile and run the following
code. The code TT(int[]) should be read out loud as int array indicating
the variable a is an int array, also known as an array of ints. Note
that the first value of the for loop below is zero. This is because in
J.T.W. and Java, the first index of an array is zero not one. This
convention harks back to the old days of the C Programming Language
and is used because it is more efficient in the low level of machine
language than counting arrays from one.

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

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

@example
a = @{ 4,5,6 @}; COMM(// Compilation error)
@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
var int[] temp = @{ 4,5,6 @};
a = temp; COMM(// Array "a" now holds 4 5 6)
@end example

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

BO(Question 4.7.2:) Write a FUNCTION TT(print) that takes an int array
argument and prints out the array. You will need to use the TT(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) FUNCTION.

@example
VAR int[] a = @{ 1,2,3 @};
print(a);
@end example

BO(Question 4.7.3:) Write a FUNCTION with same name as the previous print
FUNCTION, except that this one should take an argument that is a
double[], also known as a double array. Two functions with the same
name in the same class is allowed in Java and the practice of using
has a special name that is: EM(FUNCTION name overloading). Overloading
is only allowed when the two functions with the same name have
different parameters. When you call an overloaded FUNCTION J.T.W. and
Java looks at the number and types of the arguments a determines from
this which of the overloaded functions to call. Change the MAIN
FUNCTION to what follows so that it initializes an array of
double-precision floating point variables and then calls the second
print FUNCTION.

@example
VAR double[] b = @{ 1.1,2.2,3.3 @};
print(b);
@end example

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

Here is an example of a second initialization syntax. For this
particular example it is better to use the simpler, earlier
initialization 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 int[] a = NEW int[3]; )
PD COMM(// at this point the array is all zeroes)
PD FUR (var int i=0; i<3; i=i+1)
PD BEGIN
PD PD a[i] = i;
PD END
PD print(a);
ENDMAIN
@end example

BO(Question 4.7.4:) Write a FUNCTION create takes one INT argument,
the size of the array to create and returns an INT array of that
size. Make it so the ith element of the array is initialized to
i. Call this FUNCTION from the MAIN FUNCTION like so:

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

BO(Question 4.7.5:) Write a FUNCTION create2 takes one int argument, the
size of the array to create and returns a double array of that
size. Make it so the ith element of the array is initialized to
i.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 double[] a = create2(3);
PD print(a);
ENDMAIN
@end example

BO(Question 4.7.6:) Write a FUNCTION doubler that takes an int array x and
returns a new int array result that is twice as big as x. Copy x into
result before you return it. The extra elements in the result should
all be zero.

BO(Question 4.7.7:) Change the doubler FUNCTION so that every zero in the
array result is set to the value 13.

@subsection Two dimensional non-Object arrays

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

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

@example
BEGINMAIN
PD VAR int[][] a = @{ @{ 1,2,3 @} @{ 4,5 @} @{ 6 @} @}

PD FUR (var int y=0; y<a.length; y=y+1)
PD BEGIN
PD PD FUR (var int x=0; x<a[y].length; x=x+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.7.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 ints, the other taken a two
dimensional array of doubles. The call both of these functions from
the MAIN FUNCTION.

@cindex Function name overloading

Note that if x is a two dimensional array of ints, then x[i] is a one
dimensional array of ints for each in the range 0 ... x.length-1. Note
that in the above code, a[0] is an array of three ints, a[1] is an
array of two ints and a[2] is an array of one int. The reason these
sub-arrays are all of different sizes is to save your computer's
precious memory. For example you can have one sub-array much longer
than all of the others without needing to allocate a whole bunch of
memory that will go unused. Since a[0] is an int array, you would
naively expect it to be able to be re-initialized like so:

@example
a[0] = @{ 4,5,6,7@};)
@end example

so that after this code TT(a[0]) holds the four element long array
4,5,6 and 7. But as mentioned above in @ref{Single-dimensional
non-Object arrays}, this doesn't work because of 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 int[] temp = @{ 4,5,6,7@};
a[0] = temp;) COMM(// Array "a[0]" now holds 4 5 6 7)
@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 a[0], a[1] and a[2] are all of equal size, namely
three.

@example
VAR int[][] a = new int[3][3];
a[0][0] = 1; a[1][0] = 2; a[2][0] = 3;
a[0][1] = 4; a[1][1] = 5;
a[0][2] = 6;
@end example

BO(Question 4.7.10:) Write a FUNCTION create3 and create4 that takes on int
argument size and returns a two dimensional array of ints or doubles,
respectively. Make is so that if a is the name of the returned array,
then a[y][x] is set to the value of x+y.

@subsection Three-dimensional non-Object arrays

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

BO(Question 4.7.11:) Using the knowledge you have gained so far about
arrays, create, initialize and print a three-dimensional array of
ints.

m4_changequote([,])
