Next: Tutorial 11 References to another class, Previous: Tutorial 9 Mapping class variables to instance variables (also known as properties) and functions to methods, Up: J.T.W. Tutorials [Contents][Index]
This tutorial teaches you how to create single-dimensional and
multi-dimensional arrays of Object
s. The Object
types are all
types execept for boolean, char, int, float and double. A helpful
convention in Java is that the Object
types start with an uppercase
letter, while non-Object
types start with a lowercase 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.
Object
sQuestion 4.10.1: Here is an example of a convenient one-dimensional
array initialization syntax. Study, compile and run the following
code. The code Person
[] should be read out loud as “person
array” indicating the variable a
is a person array, also known as
an “array of persons”.
class
Personbegin
private
property
String name;constructor
Person(String aName)begin
name = aName;end
public
String toString()begin
return
name;end
end
class
PersonTestbegin
beginMain
var
Person[] a = { new Person("Person # 1"), new Person("Person # 2"), new Person("Person # 3") };superfor
(var
int i=0to
a.length-1)begin
System.out.println("a[" + i + "]=" + a[i]);end
endMain
end
Due to a design oversight by the creators of Java you cannot use this syntax to re-initialize an array like so:
// Compilation error a = { new Person("Person # 4"), new Person("Person # 5"), new Person("Person # 6"), new Person("Person # 7") };
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:
// No error
var
Person[] temp = { new Person("Person # 4"), new Person("Person # 5"), new Person("Person # 6"), new Person("Person # 7") };
a = temp; // Array "a" now holds Person # 4,Person # 5,Person # 6,Person # 7
Later you will learn why this design pattern is useful for re-initializing multi-dimensional arrays.
Question 4.10.2: Write a function
in the class
PersonTest called
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 print
x
function
.
var
Person[] a = { new Person("Person # 1"), new Person("Person # 2"), new Person("Person # 3")};
print(a);
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
.
var
Mine[] b = { new Mine(1), new Mine(2), new Mine(3) };
print(b);
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.
beginMain
var
Person[] a = new Person[3]; // at this point the array is all nullssuperfor
(var
int i=0to
a.length-1)begin
a[i] = new Person("Person # " + (i+1));end
print(a);endMain
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:
beginMain
var
Person[] a = create(3); print(a);endMain
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:
beginMain
var
Mine[] a = create2(3); print(a);endMain
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
.
Question 4.10.7: Change the doubler function
so that every null
in the
array result is set to a new
Person
make it so that every new Person
Object
has a different name property.
Object
sQuestion 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 a
is a person array array, also
known as a two-dimensional array of persons.
beginMain
var
Person[][] a = { { new Person("Person # 1"), new Person("Person # 2"), new Person("Person # 3") }, { new Person("Person # 4"), new Person("Person # 5") }, { new Person("Person # 6") } };superfor
(var
int y=0to
a.length-1)begin
superfor
(var
int x=0;to
a[y].length-1)begin
System.out.print(" " + a[y][x]);end
System.out.println();end
endMain
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:
a[0] = { new Person("Person # 4"), new Person("Person # 5"), new Person("Person # 6") };
so that after this code a0 holds the four element long array Person
#4
,Person #5
and 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:
var
Person[] temp = { new Person("Person # 4"),
new Person("Person # 5"),
new Person("Person # 6") };
a[0] = temp; // Array "a[0]" now holds Person # 4,Person # 5,Person # 6
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.
beginMain
var
Person[][] a = new Person[3][3]; a[0][0] = new Person("Person # 1"); a[0][1] = new Person("Person # 2"); a[0][2] = new Person("Person # 3"); a[1][0] = new Person("Person # 4"); a[1][1] = new Person("Person # 5"); a[1][2] = new Person("Person # 6"); a[2][0] = new Person("Person # 7"); a[2][1] = new Person("Person # 8"); a[2][2] = new Person("Person # 9");endMain
Question 4.10.10: Write a function
create3 and create4
that takes an int
argument size and returns a two-dimensional array of
Person
or Mine
, respectively. Make is so that each Person
or
Mine
Object
has its own number, using a separate counter
variable
.
var
int
count
Object
sQuestion 4.10.11: Using the knowledge you have gained so far about
arrays, create, initialize and print a three-dimensional array of
Person
s. Make it so that each Person
Object
is given its
own number using a separate counter variable
.
var
int
count