L12-2D-Arrays.ppt

Download Report

Transcript L12-2D-Arrays.ppt

King Fahd University of Petroleum & Minerals
College of Computer Science & Engineering
Information & Computer Science Department
ICS102
Lecture 12 : 2-D Arrays
July 17, 2016
Two-Dimensional Arrays

Two-dimensional (2-D) Arrays.

2-D Array Declaration.

Initializer Lists.

Differing Numbers of Elements in each Row.

Implementation of a 2-D Array

Printing a 2-D Array.
2-D Arrays: Why?



Often data come in a 2-D form. For example:
 The layout of a printed page is 2-D
 Computer screen is 2-D
 The spread sheet (table) is 2-D
For these situations we need a 2-D array.
A 2-D array can be thought of as a collection of "slots" laid out in
a 2-D grid.
 each slot can hold a value
 two indexes are needed to specify a slot.
Example: Table of Student Grades


Imagine a class of 7 students that have a quiz grades for 5
weeks. These grades can be stored in a table form.
A particular cell of the table is identified by student number and
week number. For example:

The grade for student 0 in week 1 is 42
Student

The grade for student 3 in week 4 is 93

The grade for student 6 in week 2 is 78
Week
0
1
2
3
4
0
99
42
74
83
100
1
90
91
72
88
95
2
88
61
74
89
96
3
61
89
82
98
93
4
93
73
75
78
99
5
50
65
92
87
94
6
43
98
78
56
99
2-D Arrays in Java





In Java, a table may be implemented as a 2-D array.
As with 1-D arrays, every slot in a 2-D array is of the same type
which can be a primitive type or an object reference type.
Each slot of the array is specified with a row and column number.
Suppose that gradeTable is a 2-D array then the syntax to specify a
particular slot should be gradeTable[row][col]
For example
Student
Week
0
1
2
3
4

gradeTable[0][1] is 42
0
99
42
74
83
100
1
90
91
72
88
95

gradeTable[3][4] is 93
2
88
61
74
89
96
3
61
89
82
98
93
4
93
73
75
78
99
5
50
65
92
87
94
6
43
98
78
56
99

gradeTable[6][2] is 78
2-D Arrays in Java

The subscripted variables of a 2-D array can be used in assignment
statements and arithmetic expressions just like any variable:
gradeTable[ 0 ][ 1 ] = 33 ; // puts a 33 into row 0 column 1.
gradeTable[ 3 ][ 4 ]++; //increments the value at row 3 column 4.
int value = (gradeTable[ 6 ][ 2 ] + 2)/2;//puts 40 into value
Student

Write a Java statement that puts a
zero into row 5 column 3.
gradeTable[ 5 ][ 3 ] = 0
Week
0
1
2
3
4
0
99
33
74
83
100
1
90
91
72
88
95
2
88
61
74
89
96
3
61
89
82
98
94
4
93
73
75
78
99
5
50
65
92
87
94
6
43
98
78
56
99
2-D Array Declaration



In Java, a 2-D array is an object.
To declare a reference variable myArray to a 2-D array of int:
int[][] myArray;
To create an array object of 3 rows and 5 columns, and put the
reference in myArray, we write
int[][] myArray = new int[3][5];

All the elements of myArray are initialized to zero.

We can create the above array by using the initializer list:
int[][] myArray = {{0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}};
Different Numbers of Elements per Row

In Java, each row of a 2-D array may have a different number of
elements. In the following example, the array A has

3 elements in its first row,

2 in its second row,

and 5 in its last row.
int[][] A = {{ 1, 9, 4 }, { 0, 2}, { 0, 1, 2, 3, 4 }};
Length of a 2-D Array

The length of a 2-D array is the number of rows it has.




So the row index (number) is from 0 to length-1.
As each row in a 2-D array is like a 1-D array, we can refer to an entire row by
specifying just the row index, e.g., A[0] means row # 0
The length of a row is the number of columns in the row.
As each row of a 2-D array can have a different number of columns, so each
row has its own length.
int[][] A = { { 1, 9, 4 },
System.out.println("Length
System.out.println("Length
System.out.println("Length
System.out.println("Length
{ 0, 2}, { 0, 1, 2, 3, 4 } };
of array is: " + A.length );
of row[0] is: " + A[0].length );
of row[1] is: " + A[1].length );
of row[2] is: " + A[2].length );
Implementation of 2-D Array


A 2-D array is implemented as an array of 1-D arrays.
To understand the idea, we construct the 2-D array stepby-step.
int[][] myArray;

declares a variable myArray
myArray = new int[3][] ;

// 2
Creates an array object. The array object has 3 slots.
Each slot may refer (in the future) to an array of int
(i.e., a row)
myArray[0] = new int[3] ;

// 1
// 3
Creates a 1-D array object and puts its reference in
slot 0 of myArray.
Implementation of 2-D Array

A previously constructed 1-D array can be
assigned to a row:
int[] x = {0, 2};
int[] y = {0, 1, 2, 3, 4};
myArray[1] = x ;
myArray[2] = y ;
//4

The rows do not need to have the same
number of elements
Printing a 2-D Array

Suppose we want to print every element of a 2-D array A

It can be done by using nested loops.

The expression A[row].length gives a different number for
each row of the array.
int[][] A = {{ 1, 9, 4 }, { 0, 2}, { 0, 1, 2, 3, 4 }};
for ( int row=0; row < A.length; row++ )
{
System.out.print("Row " + row + ": ");
for ( int col=0; col < A[row].length; col++ )
System.out.print( A[row][col] + " ");
System.out.println();
}
The end
Important to do at home :
- read section 6.4 (pages 425-436)
Exercises


Write a program that creates a two dimensional
array, fills it using Scanner, and then prints the sum
of every column.
Given a two-dimensional array, dataTable, of type
double such that the rows can have different
lengths. Write a code fragment that computes the
average of each row and saves it in a singledimension array of size corresponding to the rows
of dataTable.