• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 14 pages
Class notes

Arrays and matrices

Document preview thumbnail
Preview 2 out of 14 pages

Lecture notes study book Elementary Linear Algebra of Howard Anton - ISBN: 9780470458211 (Arrays and matrices)

Content preview

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

Connected book
 image
Publisher: 2010 ISBN: 9780470458211 Edition: Unknown

Document information

Uploaded on
June 9, 2021
Number of pages
14
Written in
2020/2021
Type
Class notes
Professor(s)
Dr. chaise
Contains
All classes
$3.22

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
1
Followers
1
Items
10
Last sold
5 year ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions