CPS 1 - Spring, 2000 - Ramm 2/9/00 #11
- Announce
- Continue to Read Course Pack Chapter 3
- Quiz today
3. Numerical Computation and the Study of Functions
- Arrays, Motivation
- Aggregate Data Type
- Deal with items of same type
- Lists
- numbers
- words
- (any other type)
- Analogies
- mailboxes in PO
- CD rack with slots
- Simplifies Naming
- Allows use of Loops
- Required for:
- required for statistical problems
- required for dealing with lists
- Have Multiple Elements or Cells
- Use Subscript or Index to Identify Element
- Subscripts are usually integers in brackets
- x[5] = 21;
- ---.setText("Result is " + x[5]);
- Use Arrays in a Loop
- int k = 0; sum = 0;
- while ( k < 10 )
- {
- k = k + 1;
- sum = sum + name[k];
- }
- Array Creation and Use
- int num[] = new int[6];
- +--+--+--+--+--+--+
- | ?| ?| ?| ?| ?| ?|
- +--+--+--+--+--+--+
- --0--1--2--3--4--5-
-
- num[1] = 21; num[5] = 13;
- +--+--+--+--+--+--+
- | ?|21| ?| ?| ?|13|
- +--+--+--+--+--+--+
- --0--1--2--3--4--5-
-
-
-
-
-
- int k = 2;
- while(k<6){num[k] = k*k; k = k+1;}
- +--+--+--+--+--+--+
- | ?|21| 4| 9|16|25|
- +--+--+--+--+--+--+
- --0--1--2--3--4--5-
Setting it Up
- Declaration:
-
double weights[];
- Definition:
-
weights = new double[50];
- Or Combined:
-
double weights[] = new double[50];
Subscript Range!!!
Simple Examples
Real Array Example
Hotel.java,
Hotel.html