Lecture_07 - ENGR 2200U UOIT/FEAS Fall 2007 ENGR 2710U OO...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design 1 Chapter 7 Arrays and Array Lists Professor Saman Kumarawadu ENGR 2710U Fall 2010 – Saman Kumarawadu ENGR 2710U OO Programming and Design Chapter Goals To become familiar with using arrays and array lists To learn about wrapper classes, auto - boxing and the generalized for loop To study common array algorithms To learn how to use two - dimensional arrays To implement partially filled arrays Continued…
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design Arrays Array: An indexed structure with fixed length for storing sequence of values (elements) of the same type . (contiguous memory locations) Index: integer indicating the position of a value. Zero - based indexing (starting with zero) Constructing an array: Store in variable of type double[ ] new double[5] double[] data = new double[5]; data [0] [1] [2] [3] 4] 0.0 0.0 0.0 0.0 0.0 Default values ENGR 2710U OO Programming and Design Arrays When array is created, all values are initialized depending on array type to a default values (auto initialization): • int : 0 • double: 0.0 • boolean : false • object References: null
Background image of page 2
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design Arrays Figure 1: An Array Reference and an Array ENGR 2710U OO Programming and Design Arrays Use [ ] to access an element Figure 2: Storing a Value in an Array data[2] = 29.95;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design Arrays Using the value stored: Get array length as data.length (Not a method! - but a instance variable of array object) Index values range from 0 to length - 1 System.out.println("The value of this data item is " + data[4]); Continued… ENGR 2710U OO Programming and Design Arrays Accessing a nonexistent element results in a bounds error Array index out of bounds Limitation: Arrays have fixed length double[] data = new double[10]; data[10] = 29.95; // ERROR
Background image of page 4
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design Syntax 8.1: Array Construction new typeName [ length ] Example: new double[10] Purpose: To construct an array with a given number of elements ENGR 2710U OO Programming and Design Syntax 8.2: Array Element Access arrayReference [ index ] Example: data[2] Purpose: To access an element in an array Arrays provide random access to each value because it is allocated as a contiguous block of memory.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design Accessing an Array - Example int [] list =new int[5]; for (int i=0; i<list.length; i++){ list[i] = 2*I +1; } list 0.0 0.0 0.0 0.0 0.0 Array elements are auto- Initialized list 1 3 5 7 9 ENGR 2710U OO Programming and Design Example Write a program that asks temperature of given number of days and get console input of temperature of each day, and provide (output) temperature of each day and average temperature.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/10/2010 for the course PROGRAMMIN 2860U taught by Professor Saman during the Fall '10 term at UOIT.

Page1 / 48

Lecture_07 - ENGR 2200U UOIT/FEAS Fall 2007 ENGR 2710U OO...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online