lec18 - Lecture 18 Arrays Arrays Arrays Arrays of objects...

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

View Full Document Right Arrow Icon
CS125 Course Notes Lecture 18, Slide 1 Lecture 18 Arrays of objects Arrays as parameters Arrays as return values Arrays as instance variables (Savitch 6.2)
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS125 Course Notes Lecture 18, Slide 2 Declaring and allocating arrays of objects BaseType [] arrayName = new BaseType [ numberofEntries ]; • Note: memory allocated for array elements but individual entries are not initialized. Class name
Background image of page 2
CS125 Course Notes Lecture 18, Slide 3 Initializing array entries arrayName [ i ] = new BaseType ( arguments ); 0 <= i < length Allocate memory for object of type BaseType Constructor call
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS125 Course Notes Lecture 18, Slide 4 Array of objects example • Given: Date(int y, int m, int d) constructs a Date object with given year, month and day. • Create an array of birthdays: Date[] birthday = new Date[5]; birthday[0] = new Date(1985,9,14); birthday[1] = new Date(1987,1,11); birthday[2] = new Date(1985,6,18); birthday[3] = new Date(1986,5,13); birthday[4] = new Date(1985,12,12);
Background image of page 4
CS125 Course Notes Lecture 18, Slide 5 Memory picture 3274 14 birthday[0].day 3270 9 birthday[0].month 3266 1985 birthday[0].year 2032 5122 birthday[4] 2028 2984 birthday[3] 2024 4210 birthday[2] 2020 4118 birthday[1] 2016 3266 birthday[0] 2016 birthday
Background image of page 5

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

View Full DocumentRight Arrow Icon
CS125 Course Notes Lecture 18, Slide 6 Iterating through an array of objects • General format: for (int i = 0; i < myArray.length; i++) { // code to initialize, modify // or display array // entries. }
Background image of page 6
CS125 Course Notes Lecture 18, Slide 7 Example: initialize birthday array Date[] birthday = new Date[5]; Scanner in = new Scanner(System.in); for(int i = 0; i < birthday.length; i++) { int yr = in.nextInt(); int mon = in.nextInt(); int day = in.nextInt(); birthday[i] = new Date(yr, mon, day); }
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 25

lec18 - Lecture 18 Arrays Arrays Arrays Arrays of objects...

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

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