Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.

Similar presentations


Presentation on theme: "Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming."— Presentation transcript:

1 Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming with Arrays and Classes l Sorting Arrays l Multidimensional Arrays Arrays

2 Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 2 Multidimensional Arrays l Arrays with more than one index »number of dimensions = number of indexes l Arrays with more than two dimensions are a simple extension of two-dimensional (2-D) arrays l A 2-D array corresponds to a table or grid »one dimension is the row »the other dimension is the column »cell: an intersection of a row and column »an array element corresponds to a cell in the table

3 Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 3 Table as a 2-Dimensional Array l The table assumes a starting balance of $1000 l First dimension: row identifier - Year l Second dimension: column identifier - percentage l Cell contains balance for the year (row) and percentage (column) l Balance for year 4, rate 7.00% = $1311 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 3

4 Table as a 2-D Array l Generalizing to two indexes: [row][column] l First dimension: row index l Second dimension: column index l Cell contains balance for the year/row and percentage/column l All indexes use zero-numbering »Balance[3][4] = cell in 4th row (year = 4) and 5th column (7.50%) »Balance[3][4] = $1311 (shown in yellow) Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 4 Row Index 3 (4th row) Column Index 4 (5th column)

5 Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 5 2-D Arrays l a 2-D array is not a matrix or grid, as described in math l all arrays in Java use pointers to the array data; so 2D (or nD) arrays are actually, arrays or arrays (more later) »there are two indexes: the major and minor »the array "type" and data are stored in the minor to declare a 2-D array of int s named table[] »having 10 rows (major) and 6 columns (minor) int[][] table = new int[10][6];

6 Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 6 Method to Calculate the Cell Values Each array element corresponds to the balance for a specific number of years and a specific interest rate (assuming a starting balance of $1000): balance(starting, years, rate) = (starting) x (1 + rate) years The repeated multiplication by (1 + rate) can be done in a for loop that repeats years times. balance() method in class InterestTable

7 Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 7 Processing a 2-D Array: for Loops Nested 2-Deep Arrays and for loops are a natural fit To process all elements of an n -D array nest n for loops »each array index is controlled by it's own loop l For example: calculate and enter balances in the interest table »inner loop repeats 6 times (for six rates) for each outer loop iteration »the outer loop repeats 10 times (for 10 different values of years ) »so the inner repeats 10 x 6 = 60 times = # cells in table Excerpt from main() method of InterestTable

8 Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 8 Multidimensional Array Parameters and Returned Values l Methods may have multi-D array parameters l Methods may return a multi-D array as the value returned l The situation is similar to 1-D arrays, but with more brackets Example: a 2-D int array as a method argument Notice how the number of columns is obtained Notice how the number of rows is obtained showTable() method from class InterestTable2

9 Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 9 Multidimensional (nD) Arrays: "Arrays of Arrays" l Multidimensional arrays are actually 1D arrays of 1D arrays. Example: int[][] table = new int[3][4]; »table is a one-dimensional (1D) array of length 3 »each element in table is a 1D array with base type int. l Access a row by only using only one subscript (index): »table[0].length gives the length (4) of the first row in the array 012012 0 1 2 3 table[0] refers to the first row in the array, which is a one-dimensional array. Note: table.length (which is 3 in this case) is different than table[0].length (which is 4).

10 Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 10 Ragged Arrays l Ragged arrays have rows of unequal length »each row has a different number of columns, or entries »essentially, each element in the first index points to another 1D array of an unknown length l Ragged arrays are basic to Java, but difficult (or impossible) to implement in other languages, such as C/C++ or Visual Basic Example: create a 2-D int array named b with 5 elements in the first row, 7 in the second row, and 4 in the third row: int[][] b; b = new int[3][]; b[0] = new int[5]; b[1] = new int[7]; b[2] = new int[4];

11 Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 11 Programming Example : Employee Time Records The class TimeBook uses several arrays to keep track of employee time records: public class TimeBook { private int numberOfEmployees; private int[][] hours; private int[] weekHours; private int[] dayHours;... } hours[i][j] has the hours for employee j on day i weekHours[i] has the week's hours for employee i+1 dayHours[i] has the total hours worked by all employees on day i

12 Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 12 Programming Example : Employee Time Records Nested Loops with Multidimensional Arrays the method computeWeekHours() uses nested for loops to compute the week's total hours for each employee. each time through the outer loop body, the inner loop adds all the numbers in one column of hours[] to get the value for one element in weekHours[]. for (employeeNumber = 1; employeeNumber <= numberOfEmployees; employeeNumber++) { // Process one employee sum = 0; for (dayNumber = 0; dayNumber < 5; dayNumber++) sum = sum + hours[dayNumber][employeeNumber – 1]; weekHours[employeeNumber – 1] = sum; } hours array weekHours array 2 3 4 0 1 8 8 8 8 8 2 1 0 8 8 8 0 0 8 4 8 9 9 2 1 0 38 24 40

13 Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 13 Summary Part 1 l An array may be thought of as a collection of variables, all of the same type l An array is better thought of as a single object with a large composite value of all the elements of the array Arrays function similarly to objects created with new in a manner similar to objects discussed previously

14 Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 14 Summary Part 2 l Array indexes use zero-numbering: »start at [0]; the index i refers to the (i+1)th element; »index of the last element is (length-of-the-array - 1) »index values outside the valid range cause an "array index out of bounds" exception (error) during run-time l A method can be passed an array in a parameter and return an array »the parameter and returned array are just the memory addresses l A "partially filled array" is one in which a limited number of array elements are used—a subset of the full array »a size variable must be used to track the used elements size

15 Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 15 Summary Part 3 l A method can change the values of an array, only if the entire array (the array memory address) is passed. l Arrays can only store values of one base type (even if nD arrays); »to store more than one type of data, parallel arrays must be used »each arrays is of a different base type and the index keeps the arrays in synch l Arrays can have multiple dimensions, although… »the major dimension just points each of its elements to another single dimensional array and so one »the base type of the elements for the major dimension are memory addresses

16 Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 16 Summary Part 4 l Commonly, two-dimensional (2D) arrays can be viewed as a grid, matrix, or table with rows and columns: »one index is for the row, the other for the column »it is up to the programmer to clearly describe if the major index is the row or column (leaving the minor to be the other) l 3D, 4D, and nD arrays are just arrays or arrays »as far as Java is considered, all arrays are single dimension (1D) arrays »higher dimensions utilise the memory address as the base type for the first dimensions –the last dimension is always the base type of the arrray


Download ppt "Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming."

Similar presentations


Ads by Google