Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is an Array? Why Arrays? Programming Examples.

Similar presentations


Presentation on theme: "What is an Array? Why Arrays? Programming Examples."— Presentation transcript:

1 What is an Array? Why Arrays? Programming Examples

2 Introduction/Review Array: A variable that can contain a list of data
All the same data type Elements Indexes/Subscripts Array Length or Size Question: What is the length of an array whose indexes go from 0 to 9? 11/16/2018 Wendi Jollymore, ACES

3 Why Use Arrays? See Psuedocode in the notes: Why Use Arrays? Click!
What does this code segment do? What makes it inefficient? How would it have to be modified To process 6 courses? To process 36 courses? 11/16/2018 Wendi Jollymore, ACES

4 Creating Arrays Arrays need to be declared double[] grades;
Data type, array name, square brackets Arrays are objects! 11/16/2018 Wendi Jollymore, ACES

5 PROG 10082 - Object Oriented Programming 1
11/16/2018 Creating Arrays You need to construct the array object Then assign to the array variable double[] grades = new double[4]; Primitive arrays’ elements are initialized. Well learn about object arrays another time. 11/16/2018 Wendi Jollymore, ACES Wendi Jollymore, ACES

6 PROG 10082 - Object Oriented Programming 1
11/16/2018 Arrays In Memory The main array variable references the first element in the array object 11/16/2018 Wendi Jollymore, ACES Wendi Jollymore, ACES

7 Accessing Arrays Refer to array elements by index/subscript:
// stores values in all the elements grades[0] = 89.2; grades[1] = 75.0; grades[2] = 69.8; grades[3] = 85.5; // prints the value in the 3rd element: System.out.println(grades[2]); 11/16/2018 Wendi Jollymore, ACES

8 Exercises Do the written exercises in the notes
Try the programming exercises in the notes 11/16/2018 Wendi Jollymore, ACES

9 For-Each Loop Easy way to iterate through an array (section 6.2.7)
arrayDataType element Declaration for a local variable that holds each element value during iteration arrayVariable is the array you’re processing for (arrayDataType element: arrayVariable) { // refer to this element as "element" } 11/16/2018 Wendi Jollymore, ACES

10 For-Each Loop Example: This prints each array element on the screen
for (double g: grades) { System.out.println(g); } 11/16/2018 Wendi Jollymore, ACES

11 Exercises Do the exercises in the notes
Try the programming exercises in the notes 11/16/2018 Wendi Jollymore, ACES


Download ppt "What is an Array? Why Arrays? Programming Examples."

Similar presentations


Ads by Google