Chapter 9

29 views3 pages
Published on 8 Jul 2011
School
Ryerson University
Department
Information Technology Management
Course
ITM 200
Chapter 9 – Arrays
9.1 Problem
An array of integers is effectively a sequence of integer variables, which you can refer to
individually by giving their position in the sequence
9.2 Declaring arrays
int[] ages = new int[1000];
This code declares an array reference variable called ages, creates an array of 1000 integer
variables and assigns the array to the reference variable
Reference variable does not contain its associated data but contains a reference to the data
(effectively the address where the data is stored in the memory)
oFor example, int x; x = 5
oX is referring to int that means 5 is an integer value
Some more array reference variable declarations
oint[] intArray – reference to an array of int
ochar[] charArray – reference to an array of char
odouble[] doubleArray – reference to an array of double
You create array using the dynamic memory allocation operator new, followed by the type
of the items to go in the array for example int and the number of items (size of array) in
square brackets
oFor example, intArray = new int[100];
Automatic initialization
When an array is created, if it contains:
oNumbersthey are initialized to zero
oBooleans they are initialized to false
oReferences they are initialized to null
9.3 Accessing array elements
www.notesolution.com
Unlock document

This preview shows page 1 of the document.
Unlock all 3 pages and 3 million more documents.

Already have an account? Log in

Document Summary

9. 1 problem: an array of integers is effectively a sequence of integer variables, which you can refer to individually by giving their position in the sequence. Automatic initialization: when an array is created, if it contains, numbers they are initialized to zero, booleans they are initialized to false, references they are initialized to null. Array index: array index type must be integer, array index starts at 0, means that the index of the last element will be the number of elements in the array 1. Accessing an array element: refer to pg 201. Length: length contains the number of components in its associated array, that is the length of an array is the value of the expression. 9. 5 more on array reference variables: example on pg 203-204. Dry running programs containing arrays: dry running is a good way to understand and debug programs containing arrays. Initialize lists: when you create an array of integers, java automatically initialize them to 0.