Next: Tutorial 8 Accessing functions and class variables from another class, Previous: Tutorial 6 Class variables, Up: J.T.W. Tutorials [Contents][Index]
This tutorial teaches you how to create single-dimensional and
multi-dimensional arrays of non-Object
s. The non-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 String
class
as an example of an Object
type. In
addition to this, two different array initialization syntaxes are
presented.
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 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.
beginMain
var
int[] a = { 1,2,3 };superfor
(var
int i=0 to a.length-1)begin
System.out.println("a[" + i + "]=" + a[i]);end
endMain
Due to a design oversight by the creators of Java you cannot use this syntax to re-initialize an array like so:
a = { 4,5,6 }; // Compilation error
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:
var int[] temp = { 4,5,6 }; a = temp; // Array "a" now holds 4 5 6
Later you will learn why this design pattern is useful for re-initializing multi-dimensional arrays.
Question 4.7.2: Write a function
print
that takes an int 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 print
function
.
var
int[] a = { 1,2,3 };
print(a);
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: 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
.
var
double[] b = { 1.1,2.2,3.3 };
print(b);
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.
beginMain
var
int[] a =new
int[3]; ) // at this point the array is all zeroesfor
(var int i=0; i<3; i=i+1)begin
a[i] = i;end
print(a);endMain
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:
beginMain
var
int[] a = create(3); print(a);endMain
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:
beginMain
var
double[] a = create2(3); print(a);endMain
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.
Question 4.7.7: Change the doubler function
so that every zero in the
array result is set to the value 13.
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 int array array
indicating that the variable a
is an int array array, also
known as a two-dimensional array of ints.
beginMain
var
int[][] a = { { 1,2,3 } { 4,5 } { 6 } }for
(var int y=0; y<a.length; y=y+1)begin
for
(var int x=0; x<a[y].length; x=x+1)begin
System.out.print(" " + a[y][x]);end
System.out.println();end
endMain
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
.
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:
a[0] = { 4,5,6,7};)
so that after this code a[0]
holds the four element long array
4,5,6 and 7. But as mentioned above in 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:
var int[] temp = { 4,5,6,7}; a[0] = temp;) // Array "a[0]" now holds 4 5 6 7
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.
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;
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.
Question 4.7.11: Using the knowledge you have gained so far about arrays, create, initialize and print a three-dimensional array of ints.