Professional Documents
Culture Documents
تﺎﻓﻮﻔﺼﻤﻟا Arrays
تﺎﻓﻮﻔﺼﻤﻟا Arrays
1
اﻟﻤﺼﻔﻮﻓﺔ Array
2
ﯾﺘﻢ اﻧﺸﺎء اﻟﻤﺼﻔﻮﻓﺎت ﺑﺘﺤﺪﯾﺪ ﻧﻮع اﻟﻤﺘﻐﯿﺮ اﻟﻤﺮاد ﺗﻨﻈﯿﻤﮫ ﺿﻤﻦ اﻟﻤﺼﻔﻮﻓﺔ
واﺳﻢ اﻟﻤﺼﻔﻮﻓﺔ وﻟﻠﺘﻔﺮﻗﺔ ﯾﺠﺐ اﺿﺎﻓﺔ ﻗﻮﺳﯿﻦ ] [ وﺗﻨﺸﺄ اﻟﻤﺼﻔﻮﻓﺎت
ﻟﺘﺤﺘﻮي أي ﻧﻮع ﻣﻦ اﻟﻤﻌﻠﻮﻣﺎت اﻟﺘﻲ ﯾﻤﻜﻦ أن ﺗﺨﺰن ﻓﻲ ﻣﺘﻐﯿﺮ واﻟﯿﻚ ھﺬا
:اﻟﻤﺜﺎل
;String[] netWeight
;][String netWeight
;boolean[] GradeStudents
3
One-Dimensional Arrays
اﻟﻤﺼﻔﻮﻓﺎت أﺣﺎدﯾﺔ اﻟﺒﻌﺪ
اﻟﻤﺼﻔﻮﻓﺔ أﺣﺎدﯾﺔ اﻟﺒﻌﺪ ھﻲ اﻟﻤﺼﻔﻮﻓﺔ اﻟﺘﻲ ﻟﮭﺎ ﺑﻌﺪ واﺣﺪ ﻓﻘﻂ ـ ﺳﻄﺮ واﺣﺪ ﻣﻦ
اﻷرﻗﺎم اﻟﺘﻲ ﺗﺘﺮاوح ﺑﯿﻦ اﻟﺼﻔﺮ ورﻗﻢ اﻟﻌﻨﺼﺮ اﻷﻋﻠﻰ اﻟﻤﺤﺪد ﻟﻠﻤﺼﻔﻮﻓﺔ
وھﻞ ﻟﻠﻤﺼﻔﻮﻓﺔ ﻋﻨﺎﺻﺮ ؟
ﻧﻌﻢ واﻟﻌﻨﺎﺻﺮ ھﻨﺎ ﺑﻤﻌﻨﻰ ﻋﺪد اﻟﺒﻨﻮد اﻟﺘﻲ ﯾﻤﻜﻦ ﺗﺨﺰﯾﻨﮭﺎ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ ﻓﻜﻞ ﺑﻨﺪ
ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ ﯾﺴﻤﻰ ﻋﻨﺼﺮا وﺑﻌﺒﺎرة أﺧﺮى ﻋﻨﺪﻣﺎ ﻧﻨﺸﺊ ﻣﺼﻔﻮﻓﺔ ﻧﻘﻮم ﺑﺘﺨﺰﯾﻦ
ﻗﯿﻤﺔ أوﻟﯿﺔ ﻓﯿﮭﺎ ﺑﺎﺳﺘﻌﻤﺎل اﻟﺠﻤﻠﺔ ﻧﯿﻮ ﻣﻊ ﻧﻮع اﻟﻤﺘﻐﯿﺮ ﻛﻤﺎ ﻓﻲ اﻟﻤﺜﺎل اﻟﺘﺎﻟﻲ
;]int[] primes = new int[300
4
One-Dimensional Arrays
اﻟﻤﺼﻔﻮﻓﺎت أﺣﺎدﯾﺔ اﻟﺒﻌﺪ
Syntax to instantiate an array: طﺮﯾﻘﺔ ﻛﺘﺎﺑﺔ ﻣﺼﻔﻮﻓﺔ أﺣﺎدﯾﺔ اﻟﺒﻌﺪ
◦ dataType[ ] arrayName;
arrayName = new dataType[size]
◦ dataType[ ] arrayName = new dataType[size]
◦ dataType[ ] arrayName1, arrayName2;
◦ arrayName[indexExp]
5
Array num
int[] num = new int[5];
6
Array list
7
ﺗﻌﯿﯿﻦ ﺣﺠﻢ اﻟﻤﺼﻔﻮﻓﺔ أﺛﻨﺎء ﺗﻨﻔﯿﺬ اﻟﺒﺮﻧﺎﻣﺞ
int arraySize;
System.out.print("Enter the size of "
+ "the array: ");
arraySize = console.nextInt();
System.out.println();
int[] list = new int[arraySize];
8
Array Initialization During Declaration
double[] sales = {12.25, 32.50, 16.90,
;}23, 45.68
.ﯾﺘﻢ وﺿﻊ اﻟﻘﯿﻢ ،وﺗﺪﻋﻰ اﻟﻘﯿﻢ اﻷوﻟﯿﺔ ،ﺑﯿﻦ أﻗﻮاس وﻣﻔﺼﻮﻟﺔ ﺑﻔﻮاﺻﻞ
9
Arrays and the Instance Variable length
list.length is 6.
int[] list = {10, 20, 30, 40, 50, 60};
10
Arrays and the Instance Variable length
int[] numList = new int[10];
0 ﻋﻨﺎﺻﺮ ﻣﮭﯿﺄة ﺑﺎﻟﻘﯿﻤﺔ اﻷوﻟﯿﺔ10 ھﺬه اﻟﺨﻄﻮة ﺗﻨﺸﺊ ﻣﺼﻔﻮﻓﺔ ﻣﻦ
numList.length is 10
◦ numList[0] = 5;
◦ numList[1] = 10;
◦ numList[2] = 15;
◦ numList[3] = 20;
11
ﻣﺜﺎل ﻟﺘﺨﺰﯾﻦ ﻗﯿﻢ داﺧﻞ اﻟﻤﺼﻔﻮﻓﺔ ﻋﻦ طﺮﯾﻖ ﻗﺮاءﺗﮭﺎ
ﻣﻦ ﻗﺒﻞ اﻟﻤﺴﺘﺨﺪم
int[] list = new int[100];
int i;
12
ﺑﻌﺾ اﻟﻌﻤﻠﯿﺎت ﻋﻠﻰ اﻟﻤﺼﻔﻮﻓﺎت
ﺗﮭﯿﺌﺔ Initialize
13
double[] sales = new double[10];
int index;
double largestSale, sum, average;
14
10.00 ﺗﮭﯿﺌﺔ ﺟﻤﯿﻊ اﻟﻌﻨﺎﺻﺮ ﺑﺎﻟﻘﯿﻤﺔ
for (index = 0; index < sales.length;
index++)
sales[index] = 10.00;
15
إدﺧﺎل ﺑﯿﺎﻧﺎت إﻟﻰ اﻟﻤﺼﻔﻮﻓﺔ
for (index = 0; index < sales.length;
index++)
sales[index] = console.nextDouble();
16
طﺒﺎﻋﺔ ﻋﻨﺎﺻﺮ اﻟﻤﺼﻔﻮﻓﺔ
for (index = 0; index < sales.length;
index++)
System.out.print(sales[index] + " ");
17
اﯾﺠﺎد ﻣﺠﻤﻮع و ﻣﺘﻮﺳﻂ اﻟﻌﻨﺎﺻﺮ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ
sum = 0;
for (index = 0; index < sales.length;
index++)
sum = sum + sales[index];
if (sales.length != 0)
average = sum / sales.length;
else
average = 0.0;
18
إﯾﺠﺎد اﻟﺮﻗﻢ اﻷﻛﺒﺮ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ
maxIndex = 0;
largestSale = sales[maxIndex];
19
Determining Largest Element in Array
20
اﺳﺘﺨﺪام اﻟﻤﺼﻔﻮﻓﺔ ﻓﻲ اﻟﺪوال
General syntax to declare an array as a formal parameter:
21
ﻣﺜﺎل ﻻﺳﺘﺨﺪام داﻟﺔ ﻟﺒﯿﺎن اذا ﻣﺎ ﻛﺎﻧﺖ اﻟﻤﺼﻔﻮﻓﺘﯿﻦ
ﻣﺘﺴﺎوﯾﺘﯿﻦ أم ﻻ
boolean isEqualArrays(int[] firstArray,
int[] secondArray)
{
if (firstArray.length != secondArray.length)
return false;
for (int index = 0; index < firstArray.length;
index++)
if (firstArray[index] != secondArray[index])
return false;
return true;
}
22
foreach loop
اﻟﺸﻜﻞ اﻟﻌﺎم
)for (dataType identifier : arrayName
statements
Identifier
.ﻫﻮ ﻣﺘﻐﻴﺮ ﺑﻨﻔﺲ ﻧﻮع اﻟﻌﻨﺎﺻﺮ ﻟﻠﻤﺼﻔﻮﻓﺔ
23
foreach ﻣﺜﺎل ﻋﻠﻰ
int[] list = {10, 20, 30, 40, 50, 60};
int sum = 0;
24
Two-Dimensional Arrays
ﻣﺼﻔﻮﻓﺔ ﺛﻨﺎﺋﯿﺔ اﻟﺒﻌﺪ
inStoc
k
[GM] 10 4 95 3 5
[Ford] 6 3 6 6 4
[Toyota] 3 5 3 6 7
[BMW] 8 2 7 5 3
[Nissan] 0 4 5 33 4
[Volvo] 1 5 7 9 7
25
Two-Dimensional Arrays
ﻣﺼﻔﻮﻓﺔ ﺛﻨﺎﺋﯿﺔ اﻟﺒﻌﺪ
اﻟﺒﯿﺎﻧﺎت ﻓﻲ ﺑﻌﺾ اﻷﺣﯿﺎن ﻓﻲ ﺷﻜﻞ ﺟﺪول )ﺻﻌﺒﺔ اﻟﺘﻤﺜﯿﻞ ﺑﺎﺳﺘﺨﺪام ﻣﺼﻔﻮﻓﺔ
(أﺣﺎدﯾﺔ اﻷﺑﻌﺎد.
26
Two-Dimensional Arrays
double[][]sales = new double[10][5];
27
Accessing Two-Dimensional Array
Components
28
Two-Dimensional Arrays
int [][] matrix = new int[20][15];
20 rows ﺻﻒ
15 columns ﻋﻤﻮد
29
ﺣﺎﻟﺔ ﺧﺎﺻﺔ ﻣﻦ اﻟﻤﺼﻔﻮﻓﺔ ﺛﻨﺎﺋﯿﺔ اﻟﺒﻌﺪ
ﯾﻤﻜﻦ ﺗﻌﯿﯿﻦ ﻋﺪد أﻋﻤﺪة ﻣﻌﯿﻦ ﻓﻲ ﻛﻞ ﺻﻒ ،وﻟﻜﻦ ﻧﻌﺮف ﻛﻞ ﺻﻒ ﻋﻠﻰ
ﺣﺪة و ﺗﺴﻤﻰ
ragged arrays
30
int[] board = new int[5];
board[0] = new int[6];
board[1] = new int[2];
board[2] = new int[2];
board[3] = new int[3];
board[4] = new int[4];
31
Two Dimensional Array Initialization
During Declaration
int[][] board = {{2,3,1} , {15,25,13} , {20, 4, 7}};
boar
d
[0] [1] [2]
[0] 2 3 1
[1] 15 25 13
[2] 20 4 7
32
اﻟﻌﻤﻠﯿﺎت ﻋﻠﻰ اﻟﻤﺼﻔﻮﻓﺎت ﺛﻨﺎﺋﯿﺔ اﻟﺒﻌﺪ
Initialization اﻟﺘﮭﯿﺌﺔ
Print اﻟﻄﺒﺎﻋﺔ
35
اﻟﻌﻤﻠﯿﺎت ﻋﻠﻰ اﻟﻤﺼﻔﻮﻓﺎت ﺛﻨﺎﺋﯿﺔ اﻟﺒﻌﺪ
Largest Element in Each Row اﻟﻌﻨﺼﺮ اﻷﻛﺒﺮ ﻓﻲ ﻛﻞ ﺻﻒ
for (row = 0; row < matrix.length; row++)
{
largest = matrix[row][0];
for (col = 1; col < matrix[row].length;
col++)
if (largest < matrix[row][col])
largest = matrix[row][col];
System.out.println("The largest element of row "
+ (row + 1) + " = " + largest);
}
36
اﻟﻌﻤﻠﯿﺎت ﻋﻠﻰ اﻟﻤﺼﻔﻮﻓﺎت ﺛﻨﺎﺋﯿﺔ اﻟﺒﻌﺪ
اﻟﻌﻨﺼﺮ اﻷﻛﺒﺮ ﻓﻲ ﻛﻞ Largest Element in Each Column
ﻋﻤﻮد
ﺗﻄﺒﯿﻖ
37
Multidimensional Arrays
ﻣﺼﻔﻮﻓﺔ ﻣﺘﻌﺪدة اﻷﺑﻌﺎد
أﺑﻌﺎد أو أي ﺑﻌﺪ3 ﯾﻤﻜﻦ ﺗﻌﺮﯾﻒ ﻣﺼﻔﻮﻓﺔ ﻣﻦ
38
Loops to Process Multidimensional
Arrays
39