Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lecture 1 Wanda M. Kunkle.

Similar presentations


Presentation on theme: "Introduction to Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lecture 1 Wanda M. Kunkle."— Presentation transcript:

1 Introduction to Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lecture 1 Wanda M. Kunkle

2 2 Arrays An array is a data structure that allows us to store multiple data items of the same type. An array is a data structure that allows us to store multiple data items of the same type. The data items making up the array are stored in consecutive memory locations inside the computer. The data items making up the array are stored in consecutive memory locations inside the computer. The consecutive memory locations, or elements, all share the same name. The consecutive memory locations, or elements, all share the same name. To reference a particular element in the array, we must specify the name of the array and the position number of that element in the array. (Aside: As with Strings, we start counting with 0.) To reference a particular element in the array, we must specify the name of the array and the position number of that element in the array. (Aside: As with Strings, we start counting with 0.)

3 3 Array Example

4 4 Initialization routines Initialization routines Initial values known Initial values known int values[] = new int[5]; values[0] = 25; values[1] = 11; values[2] = 38; values[3] = 3; values[4] = 17; int values[] = new int[5]; values[0] = 25; values[1] = 11; values[2] = 38; values[3] = 3; values[4] = 17; int values[] = {25, 11, 38, 3, 17}; int values[] = {25, 11, 38, 3, 17};

5 5 Array Example Initialization routines: Initialization routines: Initial values not known Initial values not known Initialize element contents to 0 to “play it safe” Initialize element contents to 0 to “play it safe” int values[] = new int[5]; for (int i = 0; i < values.length; i++) values[i] = 0; int values[] = new int[5]; for (int i = 0; i < values.length; i++) values[i] = 0; We’ll assign real values to the array later. We’ll assign real values to the array later. The array “knows” how big it is.

6 6 Examples of Array Routines Manipulation routines: Manipulation routines: Reading in array values Reading in array values // Read grades into an array “grades” until the user enters // -1, indicating that he is done while ((grade != -1) && (counter < Size)){ out.write("Enter a grade, -1 to end: "); grade = in.readfloat(); if (grade != -1) { grades[counter] = grade; counter++; } // end if } // end while // Read grades into an array “grades” until the user enters // -1, indicating that he is done while ((grade != -1) && (counter < Size)){ out.write("Enter a grade, -1 to end: "); grade = in.readfloat(); if (grade != -1) { grades[counter] = grade; counter++; } // end if } // end while

7 7 Examples of Array Routines Manipulation routines: Manipulation routines: Printing array values Printing array values // Print the grades in array “grades” consisting of // “counter” values for (i = 0; i < counter; i++) out.writeln(grades[i]); // Print the grades in array “grades” consisting of // “counter” values for (i = 0; i < counter; i++) out.writeln(grades[i]);

8 8 Examples of Array Routines Manipulation routines: Manipulation routines: Summing array values Summing array values // Total the grades in array “grades” consisting of // “counter” values for (i = 0; i < counter; i++) total += grades[i]; // Total the grades in array “grades” consisting of // “counter” values for (i = 0; i < counter; i++) total += grades[i];

9 9 Examples of Array Routines Manipulation routines: Manipulation routines: Averaging array values Averaging array values // Average the grades in array “grades” consisting of // “counter” values and display the average average = total/counter; out.writeln("\nAverage of grades entered: " + average + "\n"); // Average the grades in array “grades” consisting of // “counter” values and display the average average = total/counter; out.writeln("\nAverage of grades entered: " + average + "\n");

10 10 Sample Program Let’s look at a sample program that demonstrates the usage of these routines: Let’s look at a sample program that demonstrates the usage of these routines: arrayRoutines.java arrayRoutines.java arrayRoutines.java


Download ppt "Introduction to Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lecture 1 Wanda M. Kunkle."

Similar presentations


Ads by Google