0% found this document useful (0 votes)
29 views29 pages

Chapter 8 - Multidimensional Arrays - Lecture22

Chapter 8_Multidimensional Arrays_Lecture22

Uploaded by

mo7roos507723
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views29 pages

Chapter 8 - Multidimensional Arrays - Lecture22

Chapter 8_Multidimensional Arrays_Lecture22

Uploaded by

mo7roos507723
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Chapter 8

Multidimensional Arrays
313
1,481 p
Lecture 22

is Eg
six
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
Lecture Outline
• Introduction
• Two-Dimensional Array Basics
– Declare/Create Two-dimensional Arrays
– Declaring, Creating, and Initializing Using Shorthand
Notations
– Lengths of Two-dimensional Arrays
• Ragged Arrays Its is
• Processing Two-Dimensional Arrays
– Initializing arrays with input values
– Initializing arrays with random values
– Printing arrays
– Summing all elements
– Summing all elements by column
– Which row has the largest sum?
‫ يمكنك استخدام مصفوفة ثنائية األبعاد لتمثيل‬.‫لقد استخدمت حتى اآلن املصفوفات أحادية البعد لنمذجة مجموعات خطية من العناصر‬
.‫مصفوفة ثنائية األبعاد‬Liang,
‫باستخدام‬ ‫بني املدن‬
Introduction ‫املسافات‬
to Java ‫الذي يصف‬
Programming, ‫التالي‬Edition,
Eleventh ‫الجدول‬ (c)‫تمثيل‬ ‫ يمكن‬، ‫املثال‬
2018 Pearson ‫ على‬.‫مصفوفة أو جدول‬
‫سبيل‬Ltd.
Education,
All rights reserved.
Motivations IN

Thus far, you have used one-dimensional arrays to model linear


collections of elements. You can use a two-dimensional array to
jp
represent a matrix or aJotable. For example, the following table that
describes the distances between the cities can be represented using a
two-dimensional array.

I IM

of asat
g

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
TH is Motivations
I
all
am a
i
7
it
Joo wi o n't 9
I
2
3
4
5
6
0 I 2 3 4 5 6
t Jw 151
7 7
Be
03 is
Sf i of
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
0
All rights reserved.
ji 0T
Objectives
● To give examples of representing data using two-dimensional arrays
(§8.1).
● To declare variables for two-dimensional arrays, create arrays, and
access array elements in a two-dimensional array using row and column
‫ و‬، ‫ قم بإنشاء مصفوفات‬، ‫لإلعالن عن متغيرات املصفوفات ثنائية األبعاد‬
indexes.(8.2§)
(§8.2).
‫الوصول إلى عناصر املصفوفة في مصفوفة ثنائية األبعاد باستخدام فهارس الصفوف والعمود‬
● To program common operations for two-dimensional arrays (displaying
arrays, summing all elements, finding the minimum and maximum
‫لبرمجة العمليات املشتركة للمصفوفات‬
_ elements) (§8.3). ، ‫ثنائية األبعاد )عرض املصفوفات‬
● To pass two-dimensional arrays to methods (§8.4). ‫ وإيجاد الحد‬، ‫وجمع كل العناصر‬
‫األدنى والحد األقصى‬
● To use multidimensional arrays (§8.8). .(8.3§) (‫العناصر‬
.(8.4§) ‫لتمرير املصفوفات ثنائية األبعاد إلى األساليب‬
.(8.8§) ‫● الستخدام املصفوفات متعددة األبعاد‬

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
Declare/Create Two-dimensional Arrays
wit Amo
// Declare array ref var
ICI e 2,81
dataType[][] refVar;
xD ng
Trays
wi's
// Create array and assign its reference to variable
refVar = new dataType[10][10];
E
a
// Combine declaration and creation in one statementE KEEN
dataType[][] refVar = new dataType[10][10];
I
// Alternative syntax go
dataType refVar[][] = new dataType[10][10];

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
Declaring Variables of Two-dimensional
Arrays and Creating Two-dimensional
Arrays S 05 238
21 8 you
int matrix[][] = new int[10][10]; lid
duobleib i 16,351
oR
int[][] matrix = new int[10][10];
Note:
ISI 81
Two subscripts are used in a two-dimensional
K array, one
a
for the row and the other for the column.
As in a one-dimensional array, the index for each subscript
is of the int type and starts from 0.
IX M1 6 213
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
9Th To i Is ga
Is
W
is s
E l
sa

v31 6,5800333 sat me If I


5,81owl 3,8161
M on it

431326 854 in IN
É I 812.03132 yes as
Two-dimensional Array Illustration
a y I
I iwi se
pyo go
7 658

I 3
a

I
of is 41 A bus
WH
ix length 5
68549
J'm Fts jpg 2.13 7
gyps
arrag.iengn.tn
Caution DID a
matrix zJ
arragCo length 3
It is a common mistake to use matrix[2, 1] to access the element at
row 2 and column 1. In Java, each subscript must be enclosed in a pair
of square brackets. 2 ‫[ للوصول إلى العنصر في الصف‬1 ، 2] ‫من الخطأ الشائع استخدام املصفوفة‬
.‫ يجب وضع كل خط منخفض في زوج من األقواس املربعة‬، Java ‫ في‬.1 ‫والعمود‬
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
0
All rights reserved.
‫التصريح والتكوين والتهيئة باستخدام الرموز املختصرة‬

Declaring, Creating, and Initializing Using


Shorthand Notations
You can also use an array initializer to declare, create
and initialize a two-dimensional array. For example,

int[][] array = { I 1 Is
int[][] array = new int[4][3];
{1, 2, 3},
a Same as
array[0][0] = 1; array[0][1] = 2; array[0][2] = 3;
{4, 5, 6}, array[1][0] = 4; array[1][1] = 5; array[1][2] = 6;
{7, 8, 9}, array[2][0] = 7; array[2][1] = 8; array[2][2] = 9;
{10, 11, 12} array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;
};

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
541 Is f
Lengths of Two-dimensional
Arrays
int[][] x = new int[3][4];
g Wl is 5 IS te bl IA
s
II 81 a s dik
ie 33 f
s wi I got a
3581

d
Is a si
A two-dimensional array is a one-dimensional array in
which each element is another one-dimensional array.
.‫املصفوفة ثنائية األبعاد هي مصفوفة أحادية البعد يكون فيها كل عنصر مصفوفة أخرى أحادية البعد‬
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
Lengths of Two-dimensional
Arrays, cont.
int[][] array = { array.length // 4
i {1, 2, 3},
array[0].length //3
21 {4, 5, 6}, 43 d
array[1].length //3
72 {7, 8, 9}, a array[2].length //3
s
43 {10, 11, 12} got array[3].length //3
};

array[4].length ArrayIndexOutOfBoundsException

05 v1
indx 2,6 aided
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
Wb twos
you 5
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 = {
matrix.length is 5
{1, 2, 3, 4, 5},
matrix[0].length is 5
{2, 3, 4, 5},
{3, 4, 5}, matrix[1].length is 4

{4, 5}, matrix[2].length is 3


{5} matrix[3].length is 2
}; matrix[4].length is 1
.‫كل صف في مصفوفة ثنائية األبعاد هو في حد ذاته مصفوفة‬
‫املجموعة‬ ‫ مثل هذه‬.‫ يمكن أن يكون للصفوف أطوال مختلفة‬، ‫لذلك‬
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
،‫ فمثال‬.‫باسم مجموعة خشنة‬ ‫يُعرف‬reserved.
All rights
0
Ragged Arrays, cont.

E in Jb l om
in
h 80,401 o

la ji
Jl d ji ji g g
w 2 I'M 1
IN DW IT i I I t.pl I
i Wd's S
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
Ragged
Arrays

You can create a ragged


array using the syntax as
follows: W
so
‫يمكنك إنشاء مصفوفة خشنة باستخدام بناء‬ iz'd
:‫الجملة على النحو التالي‬ Is a

You can now assign values


to the array. For example,
،‫ فمثال‬.‫يمكنك اآلن تعيني قيم للمصفوفة‬

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
Ragged Arrays
Note • .‫ األول‬index ‫ ][ إلنشاء مصفوفة تحديد‬int [5] ‫يتطلب بناء الجملة الجديد‬

•The syntax new int[5][ ] for creating an array


sws
requires the first index to be specified.
•The syntax new int[][] would be wrong.
‫ممنوع نكتتب الراقد اري بهذي الطريقة الزم نكتب الصفوف‬

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
I.w I
Processing Two-Dimensional Arrays
Nested for loops are often used to process a two dimensional
array. .‫غالبًا ما تُستخدم فور لوب املتداخلة ملعالجة مصفوفة ثنائية األبعاد‬
25 59 8
Imsi y
Is
Suppose an array matrix is created ass follows:
int[ ][ ] matrix = new int [10][10];
The following are some examples of processing two-
dimensional arrays: :‫فيما يلي بعض األمثلة على معالجة املصفوفات ثنائية األبعاد‬
(‫تهيئة املصفوفات بقيم اإلدخال‬

(Initializing arrays with input values) 86 I


on v53 N
(Printing arrays) ( ‫طباعة املصفوفات‬ IT
(Summing all elements) ‫جمع كل العناصر‬
(Summing all elements by column) ‫جمع كل العناصر حسب العمود‬
(Which row has the largest sum?) ‫أي صف له أكبر مجموع؟‬
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
Initializing arrays with input values
IT JG y
java.util.Scanner input = new Scanner(System.in);

System.out.println("Enter "+ matrix.length +"rows and"


+matrix[0].length + " columns: ");
i emit II a
for (int row = 0; row < matrix.length; row++) {
j
for (int column = 0; column < matrix[row].length; column++) {
0301 4
matrix[row][column] = input.nextInt();
} a
} 3101 d Joist

D
rose i t

y
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
0
All rights reserved.
Initializing arrays with random values

for (int row = 0; row < matrix.length; row++) {


for (int column = 0; column < matrix[row].length; column++) {
matrix[row][column] = (int)(Math.random()* 100);
}
} O 99 58911 a'NIO
Wow 1 112013
insist

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
a
Printing arrays
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
System.out.print(matrix[row][column] + " ");
} In j sit
W Il 1,3
System.out.println();
} Stl kids ow ID
N Jj is 81 is at H
W WI I

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
9 g I
Summing all elements
5800941 A
wig
int total = 0;
total sky
I
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
total += matrix[row][column];
} b

y't
} s s
T 3
H pit 13
no p's

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
so
sis
050155553373 5
pal
Summing elements by column
BWI 312.6
wispy
for (int column = 0; column < matrix[0].length; column++) {
int total = 0; 3
0
for (int row = 0; row < matrix.length; row++)
3 O
TE total += matrix[row][column]; IN
a
System.out.println("Sum for columne " + column + " is " + total);
}

4 was ios
8241033258 gotta Sylvia
Wl's
9 40152,8156g's 612,5 v d NYS
I 2 3
s q
81 East 4,5218 q
gg
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
yggg 0
Which row has the largest sum?

• Self study
• See textbook page 316

I
‫ للتتبع‬indexOfMaxRow ‫ و‬maxRow ‫أي صف له أكبر مجموع؟ استخدم املتغيرات‬
maxRow ‫ احسب مجموعها وح ّدث‬، ‫ لكل صف‬.‫أكبر مجموع ومؤشر للصف‬
‫مالحظة الفيديو‬
.‫ إذا كان املجموع الجديد أكبر‬indexOfMaxRow ‫و‬

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
‫أيضا إرجاع مصفوفة من دالة‪.‬‬
‫يمكنك تمرير مصفوفة ثنائية األبعاد إلى دالة تما ًما كما تمرر مصفوفة أحادية البعد‪ .‬يمكنك ً‬
‫سرد ‪ 8.1‬يعطي مثال بطريقتني‪ .‬الطريقة األولى ‪ ، () getArray ،‬ترجع مصفوفة ثنائية األبعاد والطريقة الثانية ‪sum ،‬‬
‫)‪ ، (int [] [] m‬ترجع مجموع كل العناصر في مصفوفة‪.‬‬
E Jogi sis u loll f
Passing Tow-Dimensional Arrays to
Methods
When passing a two-dimensional array to a method, the
reference of the array is passed to the method.
You can pass a two-dimensional array to a method just as you pass
a one-dimensional array. You can also return an array from a
method. Listing 8.1 gives an example with two methods. The first
method, getArray(), returns a two-dimensional array and the second
method, sum(int[][] m), returns the sum of all the elements in a
matrix.
4 m
89.3 II
PassTwoDimensionalArray Run

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
0
d

y i
in
101,8
W

s
t m am

I go.am I 9.3
‫‪IN‬‬
‫‪A‬‬

‫‪wtf‬‬
‫‪WH0,535‬‬

‫دعواتكم‬
‫أختكم‪:‬عزيزه الطالب‬

You might also like