Chapter 5 Arrays  By Daniel Linag Example 5.6 Copying Arrays In this example, you will see that a simple assignment cannot copy arrays in.

Download Report

Transcript Chapter 5 Arrays  By Daniel Linag Example 5.6 Copying Arrays In this example, you will see that a simple assignment cannot copy arrays in.

Chapter 5 Arrays
 By
Daniel Linag
Example 5.6
Copying Arrays
In this example, you will see that a simple
assignment cannot copy arrays in the
following program. The program simply
creates two arrays and attempts to copy one to
the other, using an assignment statement.
TestCopyArray
Run
Copying Arrays
Before the assignment
list2 = list1;
list1
After the assignment
list2 = list1;
Contents
of list1
list2
list1
Contents
of list1
list2
Contents
of list2
Garbage
Contents
of list2
Copying Arrays
Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new
int[sourceArray.length];
for (int i = 0; i < sourceArrays.length; i++)
targetArray[i] = sourceArray[i];
The arraycopy Utility
arraycopy(sourceArray, src_pos,
targetArray, tar_pos, length);
Example:
System.arraycopy(sourceArray, 0,
targetArray, 0, sourceArray.length);
Multidimensional Arrays
Declaring Variables of Multidimensional Arrays and
Creating Multidimensional Arrays
int[][] matrix = new int[10][10];
or
int matrix[][] = new int[10][10];
matrix[0][0] = 3;
for (int i=0; i<matrix.length; i++)
for (int j=0; j<matrix[i].length; j++)
{
matrix[i][j] = (int)(Math.random()*1000);
}
double[][] x;
Multidimensional Array Illustration
0 1
2
3
4
0 1
2
3
4
0
0
0
0
1
1
1
2
2
2
3
3
4
4
matrix = new int[5][5];
7
matrix[2][1] = 7;
3
1
1
2
2
3
4
5
6
7
8
9
10
11
12
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
Declaring, Creating, and Initializing Using
Shorthand Notations
You can also use a shorthand notation to declare, create and
initialize a two-dimensional array. For example,
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
This is equivalent to the following statements:
int[][] array
array[0][0] =
array[1][0] =
array[2][0] =
array[3][0] =
= new int[4][3];
1; array[0][1] = 2; array[0][2] =
4; array[1][1] = 5; array[1][2] =
7; array[2][1] = 8; array[2][2] =
10; array[3][1] = 11; array[3][2]
3;
6;
9;
= 12;
Lengths of Multidimensional
Arrays
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
array.length
array[0].length
array[1].length
array[2].length
Ragged Arrays
Each row in a two-dimensional array is
itself an array. So, the rows can have
different lengths. Such an array is
known as a ragged array. For example,
int[][] matrix = {
{1, 2, 3, 4, 5},
{2, 3, 4, 5},
{3, 4, 5},
{4, 5},
{5}
};
Example 5.7
Adding and Multiplying Two Matrices
 Objective:
Use two-dimensional arrays to
create two matrices, and then add and multiply
the two matrices.
 a11 a12 a13 a14 a15 
 b11 b12 b13 b14 b15 
 a11  b11





a
21 a 22 a 23 a 24 a 25
b
21 b 22 b 23 b 24 b 25




 a 21  b21
 a 31 a 32 a 33 a 34 a 35    b31 b32 b33 b34 b35    a 31  b31





 a 41 a 42 a 43 a 44 a 45 
 b41 b42 b43 b44 b45 
 a 41  b41
 a 51 a 52 a 53 a 54 a 55 
 b51 b52 b53 b54 b55 
 a 51  b51





a12  b12 a13  b13 a14  b14 a15  b15 

a 22  b22 a 23  b23 a 24  b24 a 25  b25 
a 32  b32 a 33  b33 a 34  b34 a 35  b35 

a 42  b42 a 43  b43 a 44  b44 a 45  b45 
a 52  b52 a 53  b53 a 54  b54 a 55  b55 
TestMatrixOperation
Run
Example 5.7 (cont) Adding and
Multiplying Two Matrices
 a11 a12 a13 a14 a15 
 b11 b12 b13 b14 b15 
 c11 c12 c13 c14 c15 






 a 21 a 22 a 23 a 24 a 25 
 b21 b22 b23 b24 b25 
 c 21 c 22 c 23 c 24 c 25 
 a 31 a 32 a 33 a 34 a 35    b31 b32 b33 b34 b35    c 31 c 32 c 33 c 34 c 35 






 a 41 a 42 a 43 a 44 a 45 
 b41 b42 b43 b44 b45 
 c 41 c 42 c 43 c 44 c 45 
 a 51 a 52 a 53 a 54 a 55 
 b51 b52 b53 b54 b55 
 c 51 c 52 c 53 c 54 c 55 






cij = ai1b1j+ai2b2j+ai3b3j+ai4b4j+ai5b5j
Example 5.8
Grading Multiple-Choice Test

Students’ Answers to the Questions:
0 1 2 3 4 5 6 7 8 9
Student
Student
Student
Student
Student
Student
Student
Student
0
1
2
3
4
5
6
7
A
D
E
C
A
B
B
E
B
B
D
B
B
B
B
B
A
A
D
A
D
E
A
E
C
B
A
E
C
C
C
C
C
C
C
D
C
C
C
C
D
A
B
C
D
D
D
D
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
A
A
A
A
A
A
A
A
D
D
D
D
D
D
D
D
Objective: write a
program that grades
multiple-choice test.
Key to the Questions:
0 1 2 3 4 5 6 7 8 9
Key
D B D C C D A E A D
Grade Exam
Run
Example 5.9
Calculating Total Scores

Objective: write a program that calculates the total score for
students in a class. Suppose the scores are stored in a threedimensional array named scores. The first index in scores refers to
a student, the second refers to an exam, and the third refers to the
part of the exam. Suppose there are 7 students, 5 exams, and each
exam has two parts--the multiple-choice part and the programming
part. So, scores[i][j][0] represents the score on the multiple-choice
part for the i’s student on the j’s exam. Your program displays the
total score for each student, .
TotalScore
Run