SECTION(7, Tutorial 7)
m5_question(This tutorial teaches you how to create single
dimensional and multi-dimensional arrays of non-objects. The
non-object types in Java are those which aren't declared inside a
class, so it includes the following types: EM(boolean), EM(char),
EM(int), EM(float) and EM(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
STRING class as an example of an object type. In addition to
this, two different array initialisation syntaxes are presented.)
SUBSECTION(7.1, Single dimensional arrays)
m5_question(BO(Question 7.1:) Here is an example of a convenient one
dimensional array initialisation syntax. Study, compile and run the
following code. The code CLSS(int)TT([[[]]]) should be read out loud
as EM(int array) indicating the the variable TT(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 S.J.S. and Java, the first index
of an array is zero not one. This convention harks back to the old
days of the EM(C Programming Language) and is used because it is more
efficient in the low level of machine language than counting arrays
from one. m4_ignore(Also note that parenthesis are used to delimit
arrays. I use this practice because this is the only place in Java
where a semicolon follows a closing parenthesis. If you don't know
what I am talking about, simply ignore that remark!)
m4_begin_indent
NU()VAR INT[] VARI(a) = SQU NUMB(1),NUMB(2),NUMB(3) GLY; EOL
FUR (VAR INT VARI(i)=NUMB(0); LT(i,NUMB(3)); i=i+NUMB(1)) EOL
BEGIN EOL
PD SYSTEM_OUT_PRINTLN(STRI("a[") + i + STRI("]=") + a[i]); EOL
END EOL
m4_end_indent
NU()Due to a design oversight by the creators of Java you cannot use
this syntax to re-initialise an array like so:
m4_begin_indent
NU()a = SQU NUMB(4),NUMB(5),NUMB(6) GLY; COMM(// Compilation error) EOL
m4_end_indent
NU()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:
m4_begin_indent
NU()VAR INT[] VARI(temp) = SQU NUMB(4),NUMB(5),NUMB(6) GLY; EOL
a = temp; COMM(// Array "a" now holds 4 5 6) EOL
m4_end_indent
NU()Later you will learn why this design pattern is useful for
re-initialising multi-dimensional arrays.)
m5_question(BO(Question 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.
m4_begin_indent
NU()VAR INT[] VARI(a) = SQU NUMB(1),NUMB(2),NUMB(3) GLY; EOL
print(a); EOL
m4_end_indent
)
m5_question(BO(Question 7.3:) Write a function with same name as the
previous TT(print) function, except that this one should take an
argument that is a CLSS(double)TT([[[]]]), 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 S.J.S. 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 initialises an
array of double-precision floating point variables and then calls the
second TT(print) function.
m4_begin_indent
NU()VAR DOUBLE[] VARI(b) = SQU NUMB(1.1),NUMB(2.2),NUMB(3.3) GLY; EOL
print(b); EOL
m4_end_indent
)
m5_question(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.
m4_begin_indent
NU()BEGIN_MAIN EOL
PD VAR INT[] VARI(a) = NEW INT[NUMB(3)]; EOL
PD COMM(// at this point the array is all zeroes) EOL
PD FUR (VAR INT VARI(i)=NUMB(0); LT(i,NUMB(3)); i=i+NUMB(1)) EOL
PD BEGIN EOL
PD PD a[i] = i; EOL
PD END EOL
PD print(a); EOL
END_MAIN EOL
m4_end_indent
)
m5_question(BO(Question 7.4:) Write a function TT(create) takes one
int argument, the size of the array to create and returns an int array
of that size. Make it so the TT(i)SUP(th) element of the array is
initialised to TT(i). Call this function from the MAIN function like
so:
m4_begin_indent
NU()BEGIN_MAIN EOL
PD VAR INT[] VARI(a) = create(NUMB(3)); EOL
PD print(a); EOL
END_MAIN EOL
m4_end_indent
)
m5_question(BO(Question 7.5:) Write a function TT(create2) takes one
int argument, the size of the array to create and returns a double
array of that size. Make it so the TT(i)SUP(th) element of the array
is initialised to TT(i.i). Why is it not possible to overload that
TT(create) function? Try it and see what the compiler says. Call
TT(create2) from the MAIN function like so:
m4_begin_indent
NU()BEGIN_MAIN EOL
PD VAR DOUBLE[] VARI(a) = create2(NUMB(3)); EOL
PD print(a); EOL
END_MAIN EOL
m4_end_indent
)
m5_question(BO(Question 7.6:) Write a function TT(doubler) that
takes an int array TT(x) and returns a new int array TT(result) that
is twice as big as TT(x). Copy TT(x) into TT(result) before you
return it. The extra elements in the TT(result) should all be zero.)
m5_question(BO(Question 7.7:) Change the TT(doubler) function so
that every zero in the array TT(result) is set to the value 13.)
SUBSECTION(7.2, Two dimensional arrays)
m5_question(BO(Question 7.8:) Here is an example of a convenient two
dimensional array initialisation syntax. Study, compile and run the
following code. The code CLSS(int)TT([[[]][[]]]) should be read out
loud as EM(int array array) indicating the the variable TT(a) is an
int array array, also known as a two dimensional array of ints.
m4_begin_indent
NU()BEGIN_MAIN EOL
PD VAR INT[][] a = SQU SQU NUMB(1),NUMB(2),NUMB(3) GLY SQU NUMB(4),NUMB(5) GLY SQU NUMB(6) GLY GLY EOL
EOL
PD FUR (VAR INT VARI(y)=NUMB(0); LT(y,a.length); y=y+NUMB(1)) EOL
PD BEGIN EOL
PD PD FUR (VAR INT VARI(x)=NUMB(0); LT(x,a[y].length); x=x+NUMB(1)) EOL
PD PD BEGIN EOL
PD PD PD SYSTEM_OUT_PRINT(STRI(" ") + a[y][x]); EOL
PD PD END EOL
PD PD SYSTEM_OUT_PRINTLN(); EOL
PD END EOL
END_MAIN EOL
m4_end_indent
)
m5_question(BO(Question 7.9:) By copying the pattern of the code
above, do some more overloading of the TT(print) function by writing
two new TT(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.)
m5_question(m4_changequote(,) Note that if TT(x) is a two dimensional
array of ints, then TT(x[i]) is a one dimensional array of ints for
each in the range TT(0 ... x.length-1). Note that in the above code,
TT(a[0]) is an array of three ints, TT(a[1]) is an array of two ints
and TT(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 TT(a[0]) is an int array, you would naively expect
it to be able to be re-initialised like so:
m4_begin_indent
NU()a[NUMB(0)] = SQU NUMB(4),NUMB(5),NUMB(6),NUMB(7)GLY; EOL
m4_end_indent
NU()m4_changequote(,) so that after this code TT(a[0]) holds the four
element long array 4,5,6 and 7. But as mentioned above in
Section~SECT(7.1), this doesn't work because of a 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:
m4_begin_indent
NU()VAR INT[] VARI(temp) = SQU NUMB(4),NUMB(5),NUMB(6),NUMB(7)GLY; EOL
a[NUMB(0)] = temp; COMM(// Array "a[0]" now holds 4 5 6 7) EOL
m4_end_indent
NU()m4_changequote(,) Like with one dimensional arrays, there is a second
initialisation 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.
m4_begin_indent
NU()VAR INT[][] VARI(a) = NEW INT[NUMB(3)][NUMB(3)]; EOL
a[NUMB(0)][NUMB(0)] = NUMB(1); a[NUMB(1)][NUMB(0)] = NUMB(2); a[NUMB(2)][NUMB(0)] = NUMB(3); EOL
a[NUMB(0)][NUMB(1)] = NUMB(4); a[NUMB(1)][NUMB(1)] = NUMB(5); EOL
a[NUMB(0)][NUMB(2)] = NUMB(6); EOL
m4_end_indent
)
m5_question(BO(Question 7.10:) Write a function TT(create3) and
TT(create4) that takes on int argument TT(size) and returns a two
dimensional array of ints or doubles, respectively. Make is so that
if TT(a) is the name of the returned array, then TT(a[[[y][x]]]) is
set to the value of TT(x+y).)
SUBSECTION(7.3, Three dimensional arrays)
m5_question(BO(Question 7.11:) Using the knowledge you have gained
so far about arrays, create, initialise and print a three dimensional
array of ints.)