Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 14: Arrays June 13, 2000 Nick Vallidis. Announcements zP5 goes out today.

Similar presentations


Presentation on theme: "COMP 14: Arrays June 13, 2000 Nick Vallidis. Announcements zP5 goes out today."— Presentation transcript:

1 COMP 14: Arrays June 13, 2000 Nick Vallidis

2 Announcements zP5 goes out today

3 Review zWhat is stored in an object reference variable?  What is special about static methods?

4 Homework zP4 is due tomorrow! (it was moved back a day…) zRead 6.1-6.2

5 How do we represent... za whole bunch of variables of the same type? zFor example: ya list of grades for a class yThe prices for all the different shoes at a shoe store yThe ages of all the students at UNC

6 Arrays zAn array is a list of values that can be represented by one variable zEach value is stored at a specific, numbered position in the array zThe number corresponding to each position is called an index or subscript

7 Arrays 99 75 92 87 89 95 72 79 grades 0 1 2 3 4 5 6 7 value of grades[3] index

8 Arrays zCreate an array called grades to hold 8 integers type[] name = new type[number]; int[] grades = new int[8]; yholds 8 integers in indices 0-7 zAccess a particular value array_name[index] grades[3]

9 Arrays  If you access an element outside of the array ( grades[9] ), you’ll get an ArrayIndexOutOfBounds Exception zlength yinstance variable for all arrays yreturns number of elements in array (grades.length == 8) is true

10 Arrays Initializer Lists zLists the initial value for the elements of an array zItems are separated by commas and the list is in braces {} zThe size of the array is determined by the number of items in the list int[] scores = {87, 98, 45}

11 Arrays As Parameters zEntire array can be passed as a parameter ymethod can change elements of the array permanently ymethod cannot change the reference itself zElements of an array can be passed as parameters, too ynormal rules apply…

12 Arrays of Objects  new Class_Name[num_items] returns a reference to an array of num_items references  new Class_Name (constructor_params) returns a reference to a new object of the class Class_Name zwhen filling an array of objects, you want to assign the reference to one of the elements of the array: array[index] = new Class_Name (construct_params);

13 Example time


Download ppt "COMP 14: Arrays June 13, 2000 Nick Vallidis. Announcements zP5 goes out today."

Similar presentations


Ads by Google