Unit 7
Arrays and matrices
Summary
• Arrays as collections of elements
• Declaration of array variables
• Creation of array objects
• Access to the elements of an array object
• Expressions that represent array objects
• Parameters passed to a program
• Matrices (as arrays of arrays)
7.1 Arrays
An array object (or simply array) contains a collection of elements of the same type, each of which is indexed
(i.e., identified) by a number. A variable of type array contains a reference to an array object.
To use an array in Java we have to:
1. declare a variable of type array that allows us to refer to an array object;
2. construct the array object specifying its dimension (number of elements of the array object);
3. access the elements of the array object through the array variable in order to assign or obtain their values
(as if they were single variables).
7.2 Declaration of array variables
To use an array we first have to declare a variable that refers to the array.
Declaration of array variables
Syntax:
type [] arrayName ;
where
• type is the type of the elements of the array to which the variable we are declaring will refer
• arrayName is the name of the array variable we are declaring
Semantics:
Declares a variable arrayName that can refer to an array object with elements of type type .
Example:
int[] a; // a is a variable of type reference to an array of integers
Note that, by declaring a variable of type reference to an array we have not yet constructed the array object to
which the variable refers, and hence the array cannot be used yet.
1
, 2 UNIT 7
7.3 Creation of an array object
To use an array, we must first create it, by specifying the number of elements it should have.
Creation of an array object
Syntax:
new type [dimension ]
where
• type is the name of the type of the elements of the array object that we want to contruct
• dimension is an expression of type int that evaluates to a positive (or zero) value and that specifies the
dimension of the array
Semantics:
Creates an array object with dimension elements of type type and returns a reference to the created object.
The elements of the array are indexed from 0 to dimension -1 (see later), and each one is initialized to the
default value for type .
Example:
int[] a; // a is a variable of type array of integers
a = new int[5]; // creation of an array object with 5 elements
// of type int associated to the array variable a
After the declaration After the statement
int[] a; a = new int[5];
int[] a ? int[] a int[]
0 0
1 0
2 0
3 0
4 0
After we have created an array object associated to an array variable, it is possible to access the single elements
of the collection contained in the array. These elements are initialized to the default value for the type (for
integers, it is 0, as illustrated in the figure).
7.4 Access to the single elements of an array
Each single element of an array object can be accessed as if it were a separate variable.
Access to the single elements of an array
Syntax:
arrayName [index ]
where
• arrayName is the name of the array variable that contains a reference to the array we want to access
• index is an expression of type int with non-negative value that specifies the index of the element we
want to access.
Semantics:
Accesses the element of the array arrayName in the position specified by index to read or modify it.
If the array arrayName contains N elements, the evaluation of the expression index must return an integer in
the interval [0, N − 1]; if this is not the case, a runtime error occurs when the program is run.
Example:
c Diego Calvanese Lecture Notes for Introduction to Programming A.A. 2006/07
Arrays and matrices
Summary
• Arrays as collections of elements
• Declaration of array variables
• Creation of array objects
• Access to the elements of an array object
• Expressions that represent array objects
• Parameters passed to a program
• Matrices (as arrays of arrays)
7.1 Arrays
An array object (or simply array) contains a collection of elements of the same type, each of which is indexed
(i.e., identified) by a number. A variable of type array contains a reference to an array object.
To use an array in Java we have to:
1. declare a variable of type array that allows us to refer to an array object;
2. construct the array object specifying its dimension (number of elements of the array object);
3. access the elements of the array object through the array variable in order to assign or obtain their values
(as if they were single variables).
7.2 Declaration of array variables
To use an array we first have to declare a variable that refers to the array.
Declaration of array variables
Syntax:
type [] arrayName ;
where
• type is the type of the elements of the array to which the variable we are declaring will refer
• arrayName is the name of the array variable we are declaring
Semantics:
Declares a variable arrayName that can refer to an array object with elements of type type .
Example:
int[] a; // a is a variable of type reference to an array of integers
Note that, by declaring a variable of type reference to an array we have not yet constructed the array object to
which the variable refers, and hence the array cannot be used yet.
1
, 2 UNIT 7
7.3 Creation of an array object
To use an array, we must first create it, by specifying the number of elements it should have.
Creation of an array object
Syntax:
new type [dimension ]
where
• type is the name of the type of the elements of the array object that we want to contruct
• dimension is an expression of type int that evaluates to a positive (or zero) value and that specifies the
dimension of the array
Semantics:
Creates an array object with dimension elements of type type and returns a reference to the created object.
The elements of the array are indexed from 0 to dimension -1 (see later), and each one is initialized to the
default value for type .
Example:
int[] a; // a is a variable of type array of integers
a = new int[5]; // creation of an array object with 5 elements
// of type int associated to the array variable a
After the declaration After the statement
int[] a; a = new int[5];
int[] a ? int[] a int[]
0 0
1 0
2 0
3 0
4 0
After we have created an array object associated to an array variable, it is possible to access the single elements
of the collection contained in the array. These elements are initialized to the default value for the type (for
integers, it is 0, as illustrated in the figure).
7.4 Access to the single elements of an array
Each single element of an array object can be accessed as if it were a separate variable.
Access to the single elements of an array
Syntax:
arrayName [index ]
where
• arrayName is the name of the array variable that contains a reference to the array we want to access
• index is an expression of type int with non-negative value that specifies the index of the element we
want to access.
Semantics:
Accesses the element of the array arrayName in the position specified by index to read or modify it.
If the array arrayName contains N elements, the evaluation of the expression index must return an integer in
the interval [0, N − 1]; if this is not the case, a runtime error occurs when the program is run.
Example:
c Diego Calvanese Lecture Notes for Introduction to Programming A.A. 2006/07