Скачать презентацию CS 100 A Lect 11 6 Oct 1998

0ba950918f8c80cfdaf3b7cfaeaa9731.ppt

• Количество слайдов: 12

CS 100 A Lect. 11, 6 Oct. 1998 Introduction to Arrays • Array declaration and allocation • Subscripting • Use of arrays to store collections of data Reading in Holmes for arrays: Chapter 5 (p. 147) We’ll spend several lectures using arrays. CS 100 A, Lecture 11, 6 October 1998

The ARRAY helps solve the problem 0 g 1 90 85 2 3 4 93 40 89 g[0] is 90 g[1] is 85 g. length g[2] is 93 is the number of array elements in array g g[3] is 40 g. length = 5 g[4] is 89 in “g[4]”, 4 is the “subscript” or “index” 0 1 h 90 85 2 3 4 … 204 205 93 40 89 … 60 h. length 32 is the number of array elements in array h h. length = 206 CS 100 A, Lecture 11, 6 October 1998 3

Conventions h i j k g Represents 3 array segments or sections: segment number of values in it empty when g[h. . i-1] i-h h=i g[i. . j] j+1 -i j = i-1 g[j+1. . k-1] k-(j+1) j=k CS 100 A, Lecture 11, 6 October 1998 4

Declaration of a variable that can contain an array int g; float averages; g null averages null face null Face faces; Declaring a variable does not “allocate” or create the array of elements; it only declares a variable that can contain a reference to an array of elements. An array is much like a class in that an array variable contains a reference to an array. CS 100 A, Lecture 11, 6 October 1998 5

Allocating an array of elements g = new int [5]; g 0 1 2 Face f = new Face[206]; 0 1 2 3 3 4 f 4 null null … 204 205 . . . null What does execution of f[3]= new Face(…); do? Given allocated array f, we can reference f[0], f[1], … f[205]. But also use any expression for the subscript: f[i], f[2*i], etc. CS 100 A, Lecture 11, 6 October 1998 6