100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Class notes

Arrays and matrices

Rating
-
Sold
1
Pages
14
Uploaded on
09-06-2021
Written in
2020/2021

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










Whoops! We can’t load your doc right now. Try again or contact support.

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

Subjects

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
R52,00
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
keanuperumal

Get to know the seller

Seller avatar
keanuperumal Walter Sisulu University
View profile
Follow You need to be logged in order to follow users or courses
Sold
1
Member since
4 year
Number of followers
1
Documents
10
Last sold
4 year ago

0,0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

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

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

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

Student with book image

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

Alisha Student

Frequently asked questions