14. arrays i

17

Click here to load reader

Upload: joseph-murphy

Post on 03-Jul-2015

48 views

Category:

Education


1 download

TRANSCRIPT

Page 1: 14. Arrays I

From last time…• Please turn in Homework 2!

• We made a beautiful object-oriented night sky!

• Remember that a class has:!! 1. a name!! 2. attributes (variables)!! 3. a constructor (run via ‘new’)!! 4. and methods (actions)

Page 2: 14. Arrays I
Page 3: 14. Arrays I

Arrays I

CAP 3032

Page 4: 14. Arrays I

Rules of an Array• An array is a ordered “list” object which which

holds multiple “elements” (variables or objects)!

• It can hold only “elements” of one type!

• It cannot change size after it has been created!

• It starts at the 0th position!

• An array, like a variable, is declared & initialized

Page 5: 14. Arrays I

Memory

int x;

How does the compiler know!how much space to reserve?

Page 6: 14. Arrays I

MemoryType Bits Signed? Min Value Max Value

byte 8 Yes -128 127

short 16 Yes -32768 32767

int 32 Yes -2147483648 2147483647

long 64 Yes etc… etc…

char (UTF-16) 16 No 0 65535

Page 7: 14. Arrays I

Arrays…• are declared & initialized!

• are ordered!

• hold only elements of one type!

• cannot change size!

• start at 0!

• …why!? Because of memory management.

Page 8: 14. Arrays I

1. Array Declaration• Two parts: a type and a name (just like a variable)!

• Brackets after the variable type denote an array of that variable type:!

! int[] xpositions; // array of integers

float[] widths; // array of floats

// note that int != int[]

Page 9: 14. Arrays I

2. Array Initialization• An array is an object, so we use the new operator!

• Much like variables, we have to give the initial “state” of the array, which is the size:!

! xpositions = new int[42]; // empty array

widths = new float[78]; // empty array

// size must be an integer! new int[17.2]

Page 10: 14. Arrays I

1 & 2. One-line Array Creation

• Just like variables we can declare and initialize arrays in one line:!

int[] xpositions = new int[42];

float[] widths = new float[78];

• Compare to declaring & initializing primitives:! int xposition = 22;

float width = 32.5;

Page 11: 14. Arrays I

Getting an Element

To access an element in an array: !

arrayName[elementNumber]

int x = xpositions[0];

float myWidth = widths[13];

Page 12: 14. Arrays I

Setting an Element1. Set single elements:!! xpositions[0] = 13;

xpositions[1] = 26;

xpositions[2] = 39;

2. “Array Literal,” manually set elements:!! int[] xpositions = {13,26,39};

// this only works at initialization

Page 13: 14. Arrays I

Demo!Array Basics &!Star Coordinates

Page 14: 14. Arrays I

Iteration!• We can iterate through an array using a loop:!! for(int i = 0; i < arrayName.length; i++) {

arrayName[i] = 0; // sets each to 0

}

• We can also perform operations in the loop:!! for(int i = 0; i < arrayName.length; i++) {

arrayName[i] += 3; // increments each by 3

}

Page 15: 14. Arrays I

Demo!Drawing a Line

Page 16: 14. Arrays I
Page 17: 14. Arrays I

For next time…

• Friday: Quiz 3 (Function, Objects, & Arrays)!

• Monday: Iteration 1 Group Presentations!

• Read Shiffman, p. 153–162 (Arrays II)