350 likes | 467 Views
ITM 172. Java Programming Chapter 5 Arrays – Part A. What is an array?. An array is a data structure. A data structure is a space that can be reserved in memory for storing one or more values. For example, an int cell, a float cell, and a double cell are data structures.
E N D
ITM 172 Java Programming Chapter 5 Arrays – Part A
What is an array? • An array is a data structure. A data structure is a space that can be reserved in memory for storing one or more values. For example, an int cell, a float cell, and a double cell are data structures. • To instruct the computer to setup or allocate space for a data structure (such as an int, float, or char cell), programmers write declaration statements (e.g. int x;). • Programmers can also declare arrays. An array is a data structure that contains multiple int, float, or double cells. • For example, if your program needed to store 10 different integer values, you can declare an integer array of size 10.
Reference variables • A reference variable is a variable that contains the address of a data structure. • In Java, when we declare an array, we also declare a reference variable and assign the starting address of the array to the reference variable.
Declaring an array • The following declaration statement declares a variable of type reference-to-integer-array: int myGrades[ ];// creates a reference variable • By itself, this statement is insufficient because it declares a reference variable that doesn’t reference anything (yet). • The following declaration statement (1) allocates space in memory for an array of 7 integer cells and (2) creates (declares) a variable of type reference-to-integer-array and (3) assigns the starting address of the array to the reference variable. int [ ] myGrades = new int[7];
int [ ] myGrades = new int[7]; myGrades is a variable who contains the address of a data structure (the array). myGrades is setup in the program’s (local) memory space; the array is actually allocated memory on the ‘heap’. myGrades 00000001 The array 00000001 00000010 00000011 00000100 00000101 00000110 00000111
The size of the array • In the previous example, the size of the array is 7. • The size must be specified when the array is declared. • The size must be a constant; not a variable. • The size can’t change during program execution; the programmer must commit to a size at the beginning of the program.
Referring to the elements of the array myGrades myGrades[0] myGrades[1] myGrades[2] myGrades[3] myGrades[4] myGrades[5] myGrades[6] & of array Use the index (the number in the square brackets) to refer to the individual array elements. The first element is always [0]; the last element is (length – 1).
Declaring an array with initialization • We already learned to declare an array (actually an array reference) and allocate space in memory for the array as follows: int [ ] myGrades = new int[7]; • But if you are going to assign initial values to the array when you declare it, you can do this: int [ ] myGrades = {89, 87, 90, 93, 83}; or int [ ] myGrades = new int[ ] {89, 87, 90, 93, 83}; You don’t have to specify size if providing initial values because the compiler can count.
An array is actually an object • When you declare an array, you are actually creating an object (an instance) of class Array. • Any object of class Array has a data attribute named ‘length’ which contains the size of the array. Therefore, once you declare an array (myGrades), you can refer to the size as myGrades.length.
Example 5.X: InputtingArray(not in text) • This program declares an array of size 5 integers and prompts the user for 5 values to store in the 5 integer cells.
Example 5.1: TestArray • This program declares an integer array of size 6. • It prompts the user for the six values. • It finds the maximum value entered. • It finds the number of times the maximum occurs. • It displays the maximum and the frequency that maximum occurred.
Lab 5.2: AssignGrade • This program prompts the user for a number of students and then creates an integer array whose length = the number of students. • Then it prompts the user for numeric grades and converts them to letter grades. • The teacher is grading on a curve. To determine a student’s letter grade, if the numeric grade is within 10 of the highest grade, you get an ‘A’, if the numeric grade is within 20 of the highest grade, you get a ‘B’, within 30 for a ‘C’, within 40 for a ‘D’.
Homework J3 • 5.1 on page 226 • (Prompts the user for 10 integer values, stores them in an array, and then display array elements in reverse order).
Passing Arrays to Methods • Whole arrays and individual elements of arrays can be passed to methods as arguments. • If you pass an individual element of an array to a method, its like passing any other individual int or float value: A copy of the value is passed to the method. (If int x=5 and x is passed to a method, that means that it is actually a copy of 5 that is being passed to the method.) • If you pass a whole array to a method, you’re actually passing the address of the array, not a copy of the values stored in the array.
Call by value vs. call by reference • When individual values are passed to methods, we call that ‘call by value’: max(x, y); // ‘x’ and ‘y’ are individual int cells • When a reference variable to an object is passed to a method, we call that ‘call by reference’: max (a); // ‘a’ is a reference to an int array
Lab 5.3: TestPassArray • The following program demonstrates both call by value and call by reference. • It declares an array of size 2 int. First it passes the two individual int values to a method swap( ). Then it passes the entire array to a method swapFirstTwoInArray( ). • In the first case, swapping values in the swap( ) method has no effect on the values in the array; in the second case, swapping values in the swapFirstTwoInArray( ) method does swap the values in the array.
Lab 5.4: Deviation • This program declares and initializes an array, finds the mean and standard deviation of the array values, and prints the array values. • It contains two versions of the deviation( ) method and two versions of the mean( ) method – one for double-valued arrays and one for int-valued arrays, and a print( ) method.
Additional Practice • 5.4 on page 226 • Write a method that returns an array that is a reversal of the original array
Homework J4 • Write a program that creates an array of size 10 integers, prompts the user for the 10 values and then calls the following three methods: • findAverage( ) – calculates and displays the average value in the array. • findMinimum( ) – seeks and displays the mininum value in the array. • printArray( ) – prints the entire array. • (See next slides for sample of input and output.)
The program should prompt the user for 10 numbers (one at a time):