Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to programming in java Lecture 23 Two dimensional (2D) Arrays – Part 3.

Similar presentations


Presentation on theme: "Introduction to programming in java Lecture 23 Two dimensional (2D) Arrays – Part 3."— Presentation transcript:

1 Introduction to programming in java Lecture 23 Two dimensional (2D) Arrays – Part 3

2 Table of Student Grades Imagine a class of 7 students that have a quiz every week for 5 weeks. The instructor records the grades in a table. A particular cell of the table is designated by student number and week number. For example: – The grade for student 0 week 1 is 42 – The grade for student 3 week 4 is 93 – The grade for student 6 week 2 is 78

3 Table of Student Grades (Cont…) A compact notation for specifying a cell uses the row and column indexes like this: gradeTable[ row ][ col ] For example: – gradeTable[ 0 ][ 1 ] is 42 – gradeTable[ 3 ][ 4 ] is 93 – gradeTable[ 6 ][ 2 ] is 78

4 2D Arrays in Java Rows are numbered from 0 to N-1, where N is the number of rows Columns are numbered from 0 to M-1, where M is the number of columns. A 2D array with N rows and M columns will have N times M number of cells. However, it is possible for a 2D array to have a different number of cells in each row.

5 2D Array Declaration The declaration: – int[][] myArray ; – says that myArray is expected to hold a reference to a 2D array of int. Without any further initialization, it starts out holding null. The declaration: – int[][] myArray = new int[3][5] ; – All the cells of the array are initialized to zero. The declaration: – int[][] myArray = { {8,1,2,2,9}, {1,9,4,0,3}, {0,3,0,0,7} }; creates an array of the same dimensions (same number of rows and columns) as the previous array and initializes the cells to specific values.

6 2D Array of int

7 Different Numbers of Cells per Row Each row of a 2D array may have a different number of cells. In the following example, the array uneven has – 3 cells in its first row, – 2 in its second row, – and 5 in its last row.

8 Length of a 2D Array The length of a 2D array is the number of rows it has. What does the program print out? Answer: 3

9 Length of each Row Each row of a 2D array can have a different number of cells, so each row has its own length:

10 Printing a 2D Array

11 Review Given the following: int [][] myArray = new int [4][5]; The array myArray has ____ rows and ____ columns. a)4,4 b)5,4 c)4,5 d)5,5

12 Review (Cont…) Which of the following is not a valid two- dimensional array definition? a)Date [][] ex = new Date [2][100]; b)long [] yo = new long [3][4]; c)String [][] thing = new String [10][10]; d)float [][] why = new float [1][1]; e)double [][] d = new double [1][2];

13 Review (Cont…) Which of the following array definitions defines an array with three rows and two columns? a)int [][] x = {{5, 5}, {5, 5}}; b)int [][] x = {{5, 5, 5}, {5, 5, 5}}; c)int [][] x = {{5, 5}, {5, 5}, {5, 5}}; d)int [][] x = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}; e)int [][] x = {{5, 5, 5}, {5, 5, 5}, {5, 5, 5}};

14 Review (Cont…) Given the following: double [][] array = {{3.4, 6.2, 8.1, 9.3, 7.2}, {5.5, 2.9, 8.5, 9.8, 1.1}}; What is array[1].length? a)The number of columns in row 1. b)The number of rows in the array. c)An error. d)The number of columns in the array. e)The number of columns in row 0.

15 Review (Cont…) Given the following: int[][] items = { {0, 1, 3, 4}, {4, 3, 99, 0, 7 }, {3, 2} } ; Which of the following statements replaces the 99 with 77? a)items[1][2] = 77; b)items[2][1] = 77; c)items[ 99 ] = 77; d)items[2][3] = 77;

16 Review (Cont…) Examine the following: double[][] values = { {1.2, 9.0, 3.2}, {9.2, 0.5, 1.5, -1.2}, {7.3, 7.9, 4.8} } ; what is in values[3][0] ? a)7.3 b)7.9 c)9.2 d)There is no such array element.

17 Practice Question Write a program which calculates the sum of all the values in the following array. double [][] array = {{3.4, 6.2, 8.1, 9.3, 7.2}, {5.5, 2.9, 8.5, 9.8, 1.1}, {1.5, 2.9, 3.2, 7.1, 5.8}};

18 Homework Assignment # 3 (Total marks: 5) Submission deadline: 19 th Nov 2013 (More details: See Lecture 22)


Download ppt "Introduction to programming in java Lecture 23 Two dimensional (2D) Arrays – Part 3."

Similar presentations


Ads by Google