Presentation is loading. Please wait.

Presentation is loading. Please wait.

11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.

Similar presentations


Presentation on theme: "11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The."— Presentation transcript:

1 11 Chapter 8 ARRAYS Continued

2 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The scores each student in a class earned on a test –The batting averages of the players on a baseball team –The monthly sales produced by a salesperson

3 33 MULTI-DIMENSIONAL ARRAYS Sometimes we need to be able to store multiple sets of data. For example: –The scores the students in a class earned on three exams –The batting averages of the players on a baseball team, for every team in the league –The monthly sales generated by each salesperson in a company –A table of data –A matrix representing the pixels of an image Multiple sets of data can be stored in a multi-dimensional array.

4 44 MULTI-DIMENSIONAL ARRAYS When declaring a reference variable that will hold the address of an n-dimensional array you must have n brackets after the data type of the individual elements. When using the new operator to create an n-dimensional array there must be a bracketed size declarator for each of the n dimensions.

5 55 MULTI-DIMENSIONAL ARRAYS Example: The two brackets after the key word float specify that the variable battAvgs is a reference to a two-dimensional array of floats. The new operator is used to create an array of floats with NUM_TEAMS elements in the first dimension and NUM_PLAYERS elements in the second dimension. In total there are NUM_TEAMS * NUM_PLAYERS (1000) elements in the array. final int NUM_TEAMS = 40; final int NUM_PLAYERS = 25; float[ ][ ] battAvgs = new float[NUM_TEAMS][NUM_PLAYERS]; Each size declarator is in its own set of brackets.

6 66 MULTI-DIMENSIONAL ARRAYS To access a particular element (variable in the array) we must specify its subscript/offset in each dimension. Given the declarations below, the valid subscripts in the first dimension are 0 through 39 and the valid subscripts in the second dimension are 0 through 24. final int NUM_TEAMS = 40; final int NUM_PLAYERS = 25; float[ ][ ] battAvgs = new float[NUM_TEAMS][NUM_PLAYERS]; battAvgs[0][0] is the element at subscript/offset zero in both dimensions. It corresponds to the first team’s first player.

7 77 MULTI-DIMENSIONAL ARRAYS Problem: Given the following declarations, how many dimensions does the array referenced by temperatures have? _________________ How many elements does the array have? _________________ What are the valid subscript ranges in each dimension? _________________ final int NUM_YEARS = 10; final int NUM_DAYS = 365; final int NUM_HOURS = 24; double[ ][ ][ ]temperatures = new double[NUM_YEARS][NUM_DAYS][NUM_HOURS];

8 88 MULTI-DIMENSIONAL ARRAYS Each element of an array can be used just like an individual variable of the same data type.

9 99 MULTI-DIMENSIONAL ARRAYS Problem: Write a statement to assign the value 91.5 to the element that corresponds to the sixth year, the 208th day, and the 15th hour, of the array referenced by temperatures that is declared below. final int NUM_YEARS = 10; final int NUM_DAYS = 365; final int NUM_HOURS = 24; double[ ][ ][ ]temperatures = new double[NUM_YEARS][NUM_DAYS][NUM_HOURS];

10 10 MULTI-DIMENSIONAL ARRAYS Problem: Write a statement to display the temperature stored in the array referenced by temperatures for the last hour of the 10th day of the first year. final int NUM_YEARS = 10; final int NUM_DAYS = 365; final int NUM_HOURS = 24; double[ ][ ][ ]temperatures = new double[NUM_YEARS][NUM_DAYS][NUM_HOURS];

11 11 MULTI-DIMENSIONAL ARRAYS The length Attributes of a Two-dimensional Array A one-dimensional array has a length attribute that stores the number of elements in the array. A two-dimensional array has multiple length attributes.

12 12 MULTI-DIMENSIONAL ARRAYS The length attributes make sense when we visualize a two- dimensional array as an array of arrays. final int CANDIDATES = 3; final int PRECINCTS = 4; int[ ][ ] votes = new int[CANDIDATES][PRECINCTS]; This is the first dimension. There are three elements in the candidates dimension. This is the second dimension. There are four elements in the precincts dimension.

13 13 MULTI-DIMENSIONAL ARRAYS The length Attributes of a Two-dimensional Array Given the declaration of the array referenced by votes from the segment and illustration below, votes.length is the number of elements in the first dimension of the array, which in this case is three. final int CANDIDATES = 3; final int PRECINCTS = 4; int[ ][ ] votes = new int[CANDIDATES][PRECINCTS]; votes[0][0] 0 votes[0][1] 0 votes[0][2] 0 votes[0][3] 0 votes[1][0] 0 votes[1][1] 0 votes[1][2] 0 votes[1][3] 0 votes[2][0] 0 votes[2][1] 0 votes[2][2] 0 votes[2][3] 0 votes[0] address votes[1] address votes[2] address votes

14 14 MULTI-DIMENSIONAL ARRAYS The length Attributes of a Two-dimensional Array The attribute votes[0].length is the number of elements in the array referenced by votes[0], which is this case is four. The attribute votes[1].length is the number of elements in the array referenced by votes[1], which is also four in this example, etc. final int CANDIDATES = 3; final int PRECINCTS = 4; int[ ][ ] votes = new int[CANDIDATES][PRECINCTS]; votes[0][0] 0 votes[0][1] 0 votes[0][2] 0 votes[0][3] 0 votes[1][0] 0 votes[1][1] 0 votes[1][2] 0 votes[1][3] 0 votes[2][0] 0 votes[2][1] 0 votes[2][2] 0 votes[2][3] 0 votes[0] address votes[1] address votes[2] address votes

15 15 MULTI-DIMENSIONAL ARRAYS When you create a numeric array locally it is automatically initialized to contain all zeroes. final int CANDIDATES = 3; final int PRECINCTS = 4; int[ ][ ] votes = new int[CANDIDATES][PRECINCTS]; votes[0][0] 0 votes[0][1] 0 votes[0][2] 0 votes[0][3] 0 votes[1][0] 0 votes[1][1] 0 votes[1][2] 0 votes[1][3] 0 votes[2][0] 0 votes[2][1] 0 votes[2][2] 0 votes[2][3] 0 votes[0] address votes[1] address votes[2] address votes

16 16 MULTI-DIMENSIONAL ARRAYS To access a particular element/integer variable in the array referenced by votes, you must give the subscript in both dimensions. For example, votes[1][2] is the element at offset 1 in the first dimension and offset 2 in the second dimension. final int CANDIDATES = 3; final int PRECINCTS = 4; int[ ][ ] votes = new int[CANDIDATES][PRECINCTS]; votes[0][0] 0 votes[0][1] 0 votes[0][2] 0 votes[0][3] 0 votes[1][0] 0 votes[1][1] 0 votes[1][2] 0 votes[1][3] 0 votes[2][0] 0 votes[2][1] 0 votes[2][2] 0 votes[2][3] 0 votes[0] address votes[1] address votes[2] address votes

17 17 MULTI-DIMENSIONAL ARRAYS Passing Multi-Dimensional Arrays as Arguments to Methods A multi-dimensional array may be passed to a method as an argument. When an array is passed as an argument, we use the array name without brackets in the method call. Remember arrays are passed by reference. The parameter that receives an array argument must include one set of square brackets for each dimension.

18 18 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays Programs that manipulate the contents of multi-dimensional arrays usually use nested loops. One loop is used to cycle through the subscripts in each dimension.

19 19 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays The inner loop of a nested loop iterates more quickly than the outer loop. We need to consider which dimension we need/want to move through more quickly. These subscripts should be cycled through in the inner loop.

20 20 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays Suppose we wanted to store the votes three candidates received in four precincts in an election. We might declare a two-dimensional array referenced by votes as shown below: final int CANDIDATES = 3; final int PRECINCTS = 4; int[ ][ ] votes = new int[CANDIDATES][PRECINCTS]; The valid subscripts in the first dimension of this array are 0 - 2. The valid subscripts in the second dimension of this array are 0 - 3.

21 21 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays We could use nested loops to cycle through all the subscripts in each dimension to access each element of the array. final int CANDIDATES = 3; final int PRECINCTS = 4; int[ ][ ] votes = new int[CANDIDATES][PRECINCTS]; Do we want to move through the array like following: votes[0][0], votes[0][1], votes[0][2], votes[0][3], votes[1][0], votes[1][1], votes[1][2],... If this is the case we need to cycle through the subscripts in the PRECINCTS dimension in the inner loop. This is the way we would want to move if we wanted to find the total votes received by a candidate across all precincts.

22 22 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays final int CANDIDATES = 3; final int PRECINCTS = 4; int[ ][ ] votes = new int[CANDIDATES][PRECINCTS]; Do we want to move through the array like following: votes[0][0], votes[1][0], votes[2][0], votes[0][1], votes[1][1], votes[2][1],... If this is the case we need to cycle through the subscripts in the CANDIDATES dimension in the inner loop. This is the way we would want to move if we wanted to find the total votes cast in each precinct.

23 23 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays ***See the program ProcessVotes.java on webct

24 24 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays Problem: Define an array that can be used to store 5 test scores for 100 students. Assume that the test scores are real numbers.

25 25 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays Problem: Given your declaration from the previous problem, if we wanted to get every student’s grade on a test and put it in the array before moving on to the next test, what is the correct loop header for the inner loop?

26 26 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays Problem: Given your declaration from slide 23, if we wanted to calculate each student’s average test score, what is the correct loop header for the inner loop?

27 27 THE ArrayList class We will not cover the ArrayList class in this course. You will not be asked any questions about this class on the exam.


Download ppt "11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The."

Similar presentations


Ads by Google