logo

كيفية تخزين القيمة في المصفوفة

المصفوفة عبارة عن مجموعة من العناصر المماثلة التي لها موقع ذاكرة متجاور. المصفوفة عبارة عن مجموعة من المتغيرات المتشابهة والتي يُشار إليها باسم شائع.

سيكتشف هذا البرنامج التعليمي باختصار كيفية تخزين القيمة في مصفوفة باللغات شائعة الاستخدام.

1. لغة سي

كافة المصفوفات هي كتلة متجاورة من مواقع الذاكرة. افتراضيًا، يخزن الموضع الأدنى للمصفوفة العنصر الأول، ويخزن الموضع الأعلى البيانات الأخيرة. في لغة C، يتم الإعلان عن المصفوفة عن طريق تحديد نوع العنصر والطول الإجمالي للمصفوفة المطلوبة لتخزين البيانات.

بناء جملة للإعلان عن المصفوفة

 type arrayName [ arrSize ]; 

بناء جملة التهيئة لتخزين قيم المصفوفة

 double balance[6] = {500.0, 52.0, 63.6, 77.80, 70.10, 80.12}; 

مثال

 #include int main () { int n[ 11 ]; /* declaring an array comprising of 11 integers */ int i,j; /* initialize elements of array n to 0 */ for ( i = 0; i <11; 100 i++ ) { n[ i ]="i" + 10; * storing or initializing the array at location with element } result of element's value for (j="0;" j < 11; j++ printf('element stored position [%d]="%d
&apos;," j, n[j] ); return 0; pre> <p> <strong>Output</strong> </p> <pre> Element stored at position [0] = 10 Element stored at position [1] = 11 Element stored at position [2] = 12 Element stored at position [3] = 13 Element stored at position [4] = 14 Element stored at position [5] = 15 Element stored at position [6] = 16 Element stored at position [7] = 17 Element stored at position [8] = 18 Element stored at position [9] = 19 Element stored at position [10] = 20 </pre> <h3>Multidimensional Array in C</h3> <p>In C language, the elements of a 2 D (two-dimensional) array are accessed with the help of subscripts, i.e., the row index number and the column index number of the array.</p> <p> <strong>Syntax for declaring Array</strong> </p> <pre> int val = arr[x][y]; </pre> <p> <strong>Syntax for Initializing Two-Dimensional Arrays</strong> </p> <pre> int a[1][4] = { {4, 4, 2, 1} , /* initializers for row indexed by 0 */ {4, 5, 16, 10} , /* initializers for row indexed by 1 */ {8, 19, 10, 11} /* initializers for row indexed by 2 */ }; </pre> <p> <strong></strong> </p> <pre> #include int main () { /* declaring and initializing the array with 4 rows and 2 columns*/ int arr[4][2] = { {1,0}, {1,2}, {2,4}, {3,6}}; int i, j; /* output each array element&apos;s value */ for ( i = 0; i <4; i++ ) { for ( j="0;" < 2; j++ printf(' data stored in 2d array[%d][%d]="%d
&apos;," i,j, arr[i][j] ); } return 0; pre> <p> <strong>Output</strong> </p> <pre> Data stored in 2D array[0][0] = 1 Data stored in 2D array[0][1] = 0 Data stored in 2D array[1][0] = 1 Data stored in 2D array[1][1] = 2 Data stored in 2D array[2][0] = 2 Data stored in 2D array[2][1] = 4 Data stored in 2D array[3][0] = 3 Data stored in 2D array[3][1] = 6 </pre> <h2>2. C++ Language</h2> <p>In C++ language the user needs to specify the element type and total length of array.</p> <p> <strong>Syntax to Declare Array</strong> </p> <pre> type arrName [ arrSize ]; </pre> <p> <strong>Syntax to initialize array</strong> </p> <pre> int arr[] = { 1, 2, 3, 4, 5 } </pre> <p> <strong>Example</strong> </p> <pre> #include using namespace std; int main () { // declaring an array with 4 rows. int arr[4] = {1,7,50,6}; // traversing the output for each array value for ( int i = 0; i <4; i++ ) { cout << 'array at position[' i ']: '; arr[i]<< endl; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> array at position[0]: 1 array at position[1]: 7 array at position[2]: 50 array at position[3]: 6 </pre> <h3>Multidimensional Array</h3> <p>C++ language also enables the Multidimensional arrays.</p> <p> <strong>Syntax for initializing 2D array</strong> </p> <pre> int a[4][3] = { {0, 1, 2} , /* storing data in array for row indexed by 0 */ {42, 25, 62} , /* storing data in array for row indexed by 1 */ {18, 90, 15}, /* storing data in array for row indexed by 2 */ {21, 19, 25}, /* storing data in array for row indexed by 3 */ }; </pre> <p> <strong>Example</strong> </p> <pre> #include using namespace std; int main () { // declaring an array with 4 rows and 2 columns. int arr[4][2] = { {1,0}, {0,2}, {2,3}, {5,6}}; // traversing the output for each array value for ( int i = 0; i <4; i++ ) for ( int j="0;" < 2; j++ { cout << 'array at position[' i '][' ']: '; arr[i][j]<< endl; } return 0; pre> <p> <strong>Output</strong> </p> <pre> array at position[0][0]: 1 array at position[0][1]: 0 array at position[1][0]: 0 array at position[1][1]: 2 array at position[2][0]: 2 array at position[2][1]: 3 array at position[3][0]: 5 array at position[3][1]: 6 </pre> <h2>3. Java</h2> <p>In Java language, Arrays work differently than what they used to do in C or C++ language.</p> <h3>One-Dimensional Arrays:</h3> <p>To declare an array, the user needs to have two primary components: the type and the array&apos;s name.</p> <p>The &apos;Type&apos; refers to the elementary type of a specific array. It determines the data type of all elements that are included in the array. It comprises the array of primitive data types, unlike integers, char, float, double, etc., or it could include the user-defined data types (objects of a class) as well. Therefore, the element type for the array concludes what kind of data the array will contain.</p> <p> <strong>Syntax</strong> </p> <pre> type arr_var_name[]; OR type[] arr_var_name; OR var-name = new type [size]; </pre> <p> <strong>Store values in one-dimensional array</strong> </p> <p>Assigning values to an element in an array is similar to assigning values to scalar variables.</p> <pre> Array [index]= initializers; arr[1]= 50 arr[2]= 20 </pre> <h4>NOTE: It the array element is not assigned any value, by default it has a Null (empty) value.</h4> <p> <strong>Example</strong> </p> <pre> //Java Program to demonstrate how to initialize, store and display //values in one-dimensional arrays. class Testarray{ public static void main(String args[]){ int arr[]=new int[5];//declaration and instantiation arr[0]=20;//initialization of the array arr[1]=40; arr[2]=60; arr[3]=80; arr[4]=100; //traversing array to print the array values for(int i=0;i <arr.length;i++) system.out.println(arr[i]); }} < pre> <p> <strong>Output</strong> </p> <pre> 20 40 60 80 100 </pre> <h3>Arrays of Objects</h3> <p>An array of objects is constructed in the same way as an array of primitive type data elements.</p> <p> <strong>Example</strong> </p> <pre> // Java program to create, store and display values for one-dimensional // array of objects class Employee { public int id_no; public String name; Employee(int id_no, String name) { this.id_no = id_no; this.name = name; } } // The Elements of the array are objects of a class Employee. public class Array_Objects_Example { public static void main (String[] args) { // declares an Array of integers. Employee[] arr; // assigning space for 5 objects of type Employee. arr = new Employee[5]; // storing the value for the first element of the array arr[0] = new Employee(111,&apos;Varun&apos;); // storing the value for the second elements of the array arr[1] = new Employee(121,&apos;Sukla&apos;); // so on... arr[2] = new Employee(131,&apos;Virat&apos;); arr[3] = new Employee(141,&apos;Anuskha&apos;); arr[4] = new Employee(151,&apos;Mohit&apos;); // accessing the elements of the specified array for (int i = 0; i <arr.length; i++) system.out.println('element at ' + i : arr[i].id_no +' '+ arr[i].name); } < pre> <p> <strong>Output</strong> </p> <pre> Element at 0 : 111 Varun Element at 1 : 121 Sukla Element at 2 : 131 Virat Element at 3 : 141 Anuskha Element at 4 : 151 Mohit </pre> <h3>Multidimensional Arrays</h3> <p>Multidimensional arrays are termed &apos;arrays of arrays&apos; as they can hold each element of an array with the reference of another array. These are also known as Jagged Arrays. A multidimensional array is constructed by adding a set of square brackets ([]) per dimension.</p> <p> <strong>Syntax</strong> </p> <pre> int[][] intArray = new int[10][20]; //a 2D array or matrix int[][][] intArray = new int[10][20][10]; //a 3D array </pre> <p> <strong>Example to store values in a Multidimensional Array</strong> </p> <pre> arry[0][0]=10; arry[0][1]=20; arry[0][2]=30; arry[1][0]=40; arry[1][1]=50; arry[1][3]=60; arry[2][1]=70; arry[2][2]=80; arry[2][3]=90; </pre> <p> <strong>Example of Multidimensional Array</strong> </p> <pre> class multiDimensional_Example { public static void main(String args[]) { // declaring and storing data in 2-Dimensional array int arry[][] = { {12,17,19},{32,62,12},{37,34,32} }; // traversing and printing the 2-Dimensional array for (int i=0; i<3 3 ; i++) { for (int j="0;" < j++) system.out.print(arry[i][j] + ' '); system.out.println(); } pre> <p> <strong>Output</strong> </p> <pre> 12 17 19 32 62 12 37 34 32 </pre> <h2>4. PHP</h2> <p>PHP array is an ordered map (holds elements on the base of the key-value). It is utilized to hold multiple values of a similar data type in a single variable.</p> <p>PHP contains 3 kinds of array that are as follows:</p> <ol class="points"> <li>Indexed Array</li> <li>Associative Array</li> <li>Multidimensional Array</li> </ol> <h3>1. Indexed Array</h3> <p>PHP index is described by an integer number that begins with 0 (default value). The PHP array can store any data type, such as numbers, characters, strings, and objects. All PHP array data are allocated an index number by default.</p> <p> <strong>Syntax to store values</strong> </p> <pre> $Colour =array(&apos;Red&apos;, &apos;White&apos;, &apos;Black&apos;, &apos;Yellow&apos;); </pre> <p> <strong>Or</strong> </p> <pre> $Colour[0]=&apos;Red &apos;; $Colour[1]=&apos;White&apos;; $Colour[2]=&apos;Black&apos;; $Colour[3]=&apos;Yellow&apos;; </pre> <p> <strong>Example</strong> </p> <pre> </pre> <p> <strong>Output</strong> </p> <pre> Colours are: Red, White, Black, Yellow </pre> <h3>2. Associative Array</h3> <p>In PHP, the user can associate any specific name with each array elements using the &apos;=&gt;&apos; symbol.</p> <p> <strong>Syntax</strong> </p> <pre> $marks =array(&apos;Reema&apos;=&gt;&apos;95&apos;,&apos;John&apos;=&gt;&apos;45&apos;,&apos;Rahul&apos;=&gt;&apos;20&apos;); </pre> <p> <strong>Or</strong> </p> <pre> $marks[&apos;Reema &apos;]=&apos;95&apos;; $marks[&apos;John&apos;]=&apos;45&apos;; $marks [&apos;Rahul &apos;]=&apos;20&apos;; </pre> <p> <strong>Example</strong> </p> <pre> <?php $marks['Reema ']='95'; $marks['John']='45'; $marks ['Rahul ']='20'; echo 'Reema's Marks: '.$marks ['Reema '].' <br/>&apos;; echo &apos; John&apos;s Marks: &apos;.$marks[&apos;John&apos;].&apos; <br>&apos;; echo &apos; Rahul&apos;s Marks: &apos;.$marks [&apos;Rahul &apos;].&apos; <br>&apos;; ?&gt; </pre> <p> <strong>Output</strong> </p> <pre> Reema&apos;s Marks: 95 John&apos;s Marks: 45 Rahul&apos;s Marks: 20 </pre> <h3>3. Multidimensional Array</h3> <p>Multidimensional arrays in PHP are also termed as &apos;array of arrays&apos;. It enabled the user to store array data in a tabular format. PHP multidimensional array can be expressed in the form of a matrix which is denoted by row * column.</p> <p> <strong>Syntax</strong> </p> <pre> $emp = array ( array (1,&apos;Reema&apos;,95), array(2,&apos;john&apos;,45), array(3,&apos;rahul&apos;,20) ); </pre> <p> <strong>Example</strong> </p> <pre> <?php $emp = array ( array (1,'Reema',95), array(2,'john',45), array(3,'rahul',20) ); for ($row = 0; $row < 3; $row++) { for ($col = 0; $col < 3; $col++) { echo $emp[$row][$col].' '; } echo ' <br/>&apos;; } ?&gt; </pre> <p> <strong>Output</strong> </p> <pre> 1 Reema 95 2 john 45 3 rahul 20 </pre> <h2>5. Python</h2> <p>Python uses a module named &apos;Array&apos; to handle all the functions of Arrays in Python. It is helpful when the user wants to manipulate only particular data values. Given below are the keywords that are important to learn the concept of an array in Python:</p> <ul> <li>Element - Any data stored in an array is known an element.</li> <li>Index - Whenever an array stores any data, it has some numerical location known as index that is beneficial to identify the location of the element.</li> </ul> <p> <strong>Syntax</strong> </p> <pre> from array import * arrayName = array(typecode, [data_to_be_initialized]) </pre> <p> <strong>Example</strong> </p> <pre> import array as arry n = arry.array(&apos;i&apos;, [20, 40, 60, 80]) print(&apos;First array value:&apos;, n[0]) print(&apos;Second array value:&apos;, n[1]) print(&apos;Second last array value:&apos;, n[-1]) </pre> <p> <strong>Output</strong> </p> <pre> First array value: 20 Second array value: 40 Second last array value: 80 </pre> <hr></3></pre></arr.length;></pre></arr.length;i++)></pre></4;></pre></4;></pre></4;></pre></11;>

مصفوفة متعددة الأبعاد في لغة C

في لغة C، يتم الوصول إلى عناصر المصفوفة ثنائية الأبعاد (ثنائية الأبعاد) بمساعدة المشتركين، أي رقم فهرس الصف ورقم فهرس العمود للمصفوفة.

بناء جملة للإعلان عن المصفوفة

 int val = arr[x][y]; 

بناء الجملة لتهيئة المصفوفات ثنائية الأبعاد

 int a[1][4] = { {4, 4, 2, 1} , /* initializers for row indexed by 0 */ {4, 5, 16, 10} , /* initializers for row indexed by 1 */ {8, 19, 10, 11} /* initializers for row indexed by 2 */ }; 

 #include int main () { /* declaring and initializing the array with 4 rows and 2 columns*/ int arr[4][2] = { {1,0}, {1,2}, {2,4}, {3,6}}; int i, j; /* output each array element&apos;s value */ for ( i = 0; i <4; i++ ) { for ( j="0;" < 2; j++ printf(\' data stored in 2d array[%d][%d]="%d
&apos;," i,j, arr[i][j] ); } return 0; pre> <p> <strong>Output</strong> </p> <pre> Data stored in 2D array[0][0] = 1 Data stored in 2D array[0][1] = 0 Data stored in 2D array[1][0] = 1 Data stored in 2D array[1][1] = 2 Data stored in 2D array[2][0] = 2 Data stored in 2D array[2][1] = 4 Data stored in 2D array[3][0] = 3 Data stored in 2D array[3][1] = 6 </pre> <h2>2. C++ Language</h2> <p>In C++ language the user needs to specify the element type and total length of array.</p> <p> <strong>Syntax to Declare Array</strong> </p> <pre> type arrName [ arrSize ]; </pre> <p> <strong>Syntax to initialize array</strong> </p> <pre> int arr[] = { 1, 2, 3, 4, 5 } </pre> <p> <strong>Example</strong> </p> <pre> #include using namespace std; int main () { // declaring an array with 4 rows. int arr[4] = {1,7,50,6}; // traversing the output for each array value for ( int i = 0; i <4; i++ ) { cout << \'array at position[\' i \']: \'; arr[i]<< endl; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> array at position[0]: 1 array at position[1]: 7 array at position[2]: 50 array at position[3]: 6 </pre> <h3>Multidimensional Array</h3> <p>C++ language also enables the Multidimensional arrays.</p> <p> <strong>Syntax for initializing 2D array</strong> </p> <pre> int a[4][3] = { {0, 1, 2} , /* storing data in array for row indexed by 0 */ {42, 25, 62} , /* storing data in array for row indexed by 1 */ {18, 90, 15}, /* storing data in array for row indexed by 2 */ {21, 19, 25}, /* storing data in array for row indexed by 3 */ }; </pre> <p> <strong>Example</strong> </p> <pre> #include using namespace std; int main () { // declaring an array with 4 rows and 2 columns. int arr[4][2] = { {1,0}, {0,2}, {2,3}, {5,6}}; // traversing the output for each array value for ( int i = 0; i <4; i++ ) for ( int j="0;" < 2; j++ { cout << \'array at position[\' i \'][\' \']: \'; arr[i][j]<< endl; } return 0; pre> <p> <strong>Output</strong> </p> <pre> array at position[0][0]: 1 array at position[0][1]: 0 array at position[1][0]: 0 array at position[1][1]: 2 array at position[2][0]: 2 array at position[2][1]: 3 array at position[3][0]: 5 array at position[3][1]: 6 </pre> <h2>3. Java</h2> <p>In Java language, Arrays work differently than what they used to do in C or C++ language.</p> <h3>One-Dimensional Arrays:</h3> <p>To declare an array, the user needs to have two primary components: the type and the array&apos;s name.</p> <p>The &apos;Type&apos; refers to the elementary type of a specific array. It determines the data type of all elements that are included in the array. It comprises the array of primitive data types, unlike integers, char, float, double, etc., or it could include the user-defined data types (objects of a class) as well. Therefore, the element type for the array concludes what kind of data the array will contain.</p> <p> <strong>Syntax</strong> </p> <pre> type arr_var_name[]; OR type[] arr_var_name; OR var-name = new type [size]; </pre> <p> <strong>Store values in one-dimensional array</strong> </p> <p>Assigning values to an element in an array is similar to assigning values to scalar variables.</p> <pre> Array [index]= initializers; arr[1]= 50 arr[2]= 20 </pre> <h4>NOTE: It the array element is not assigned any value, by default it has a Null (empty) value.</h4> <p> <strong>Example</strong> </p> <pre> //Java Program to demonstrate how to initialize, store and display //values in one-dimensional arrays. class Testarray{ public static void main(String args[]){ int arr[]=new int[5];//declaration and instantiation arr[0]=20;//initialization of the array arr[1]=40; arr[2]=60; arr[3]=80; arr[4]=100; //traversing array to print the array values for(int i=0;i <arr.length;i++) system.out.println(arr[i]); }} < pre> <p> <strong>Output</strong> </p> <pre> 20 40 60 80 100 </pre> <h3>Arrays of Objects</h3> <p>An array of objects is constructed in the same way as an array of primitive type data elements.</p> <p> <strong>Example</strong> </p> <pre> // Java program to create, store and display values for one-dimensional // array of objects class Employee { public int id_no; public String name; Employee(int id_no, String name) { this.id_no = id_no; this.name = name; } } // The Elements of the array are objects of a class Employee. public class Array_Objects_Example { public static void main (String[] args) { // declares an Array of integers. Employee[] arr; // assigning space for 5 objects of type Employee. arr = new Employee[5]; // storing the value for the first element of the array arr[0] = new Employee(111,&apos;Varun&apos;); // storing the value for the second elements of the array arr[1] = new Employee(121,&apos;Sukla&apos;); // so on... arr[2] = new Employee(131,&apos;Virat&apos;); arr[3] = new Employee(141,&apos;Anuskha&apos;); arr[4] = new Employee(151,&apos;Mohit&apos;); // accessing the elements of the specified array for (int i = 0; i <arr.length; i++) system.out.println(\'element at \' + i : arr[i].id_no +\' \'+ arr[i].name); } < pre> <p> <strong>Output</strong> </p> <pre> Element at 0 : 111 Varun Element at 1 : 121 Sukla Element at 2 : 131 Virat Element at 3 : 141 Anuskha Element at 4 : 151 Mohit </pre> <h3>Multidimensional Arrays</h3> <p>Multidimensional arrays are termed &apos;arrays of arrays&apos; as they can hold each element of an array with the reference of another array. These are also known as Jagged Arrays. A multidimensional array is constructed by adding a set of square brackets ([]) per dimension.</p> <p> <strong>Syntax</strong> </p> <pre> int[][] intArray = new int[10][20]; //a 2D array or matrix int[][][] intArray = new int[10][20][10]; //a 3D array </pre> <p> <strong>Example to store values in a Multidimensional Array</strong> </p> <pre> arry[0][0]=10; arry[0][1]=20; arry[0][2]=30; arry[1][0]=40; arry[1][1]=50; arry[1][3]=60; arry[2][1]=70; arry[2][2]=80; arry[2][3]=90; </pre> <p> <strong>Example of Multidimensional Array</strong> </p> <pre> class multiDimensional_Example { public static void main(String args[]) { // declaring and storing data in 2-Dimensional array int arry[][] = { {12,17,19},{32,62,12},{37,34,32} }; // traversing and printing the 2-Dimensional array for (int i=0; i<3 3 ; i++) { for (int j="0;" < j++) system.out.print(arry[i][j] + \' \'); system.out.println(); } pre> <p> <strong>Output</strong> </p> <pre> 12 17 19 32 62 12 37 34 32 </pre> <h2>4. PHP</h2> <p>PHP array is an ordered map (holds elements on the base of the key-value). It is utilized to hold multiple values of a similar data type in a single variable.</p> <p>PHP contains 3 kinds of array that are as follows:</p> <ol class="points"> <li>Indexed Array</li> <li>Associative Array</li> <li>Multidimensional Array</li> </ol> <h3>1. Indexed Array</h3> <p>PHP index is described by an integer number that begins with 0 (default value). The PHP array can store any data type, such as numbers, characters, strings, and objects. All PHP array data are allocated an index number by default.</p> <p> <strong>Syntax to store values</strong> </p> <pre> $Colour =array(&apos;Red&apos;, &apos;White&apos;, &apos;Black&apos;, &apos;Yellow&apos;); </pre> <p> <strong>Or</strong> </p> <pre> $Colour[0]=&apos;Red &apos;; $Colour[1]=&apos;White&apos;; $Colour[2]=&apos;Black&apos;; $Colour[3]=&apos;Yellow&apos;; </pre> <p> <strong>Example</strong> </p> <pre> </pre> <p> <strong>Output</strong> </p> <pre> Colours are: Red, White, Black, Yellow </pre> <h3>2. Associative Array</h3> <p>In PHP, the user can associate any specific name with each array elements using the &apos;=&gt;&apos; symbol.</p> <p> <strong>Syntax</strong> </p> <pre> $marks =array(&apos;Reema&apos;=&gt;&apos;95&apos;,&apos;John&apos;=&gt;&apos;45&apos;,&apos;Rahul&apos;=&gt;&apos;20&apos;); </pre> <p> <strong>Or</strong> </p> <pre> $marks[&apos;Reema &apos;]=&apos;95&apos;; $marks[&apos;John&apos;]=&apos;45&apos;; $marks [&apos;Rahul &apos;]=&apos;20&apos;; </pre> <p> <strong>Example</strong> </p> <pre> <?php $marks[\'Reema \']=\'95\'; $marks[\'John\']=\'45\'; $marks [\'Rahul \']=\'20\'; echo \'Reema\'s Marks: \'.$marks [\'Reema \'].\' <br/>&apos;; echo &apos; John&apos;s Marks: &apos;.$marks[&apos;John&apos;].&apos; <br>&apos;; echo &apos; Rahul&apos;s Marks: &apos;.$marks [&apos;Rahul &apos;].&apos; <br>&apos;; ?&gt; </pre> <p> <strong>Output</strong> </p> <pre> Reema&apos;s Marks: 95 John&apos;s Marks: 45 Rahul&apos;s Marks: 20 </pre> <h3>3. Multidimensional Array</h3> <p>Multidimensional arrays in PHP are also termed as &apos;array of arrays&apos;. It enabled the user to store array data in a tabular format. PHP multidimensional array can be expressed in the form of a matrix which is denoted by row * column.</p> <p> <strong>Syntax</strong> </p> <pre> $emp = array ( array (1,&apos;Reema&apos;,95), array(2,&apos;john&apos;,45), array(3,&apos;rahul&apos;,20) ); </pre> <p> <strong>Example</strong> </p> <pre> <?php $emp = array ( array (1,\'Reema\',95), array(2,\'john\',45), array(3,\'rahul\',20) ); for ($row = 0; $row < 3; $row++) { for ($col = 0; $col < 3; $col++) { echo $emp[$row][$col].\' \'; } echo \' <br/>&apos;; } ?&gt; </pre> <p> <strong>Output</strong> </p> <pre> 1 Reema 95 2 john 45 3 rahul 20 </pre> <h2>5. Python</h2> <p>Python uses a module named &apos;Array&apos; to handle all the functions of Arrays in Python. It is helpful when the user wants to manipulate only particular data values. Given below are the keywords that are important to learn the concept of an array in Python:</p> <ul> <li>Element - Any data stored in an array is known an element.</li> <li>Index - Whenever an array stores any data, it has some numerical location known as index that is beneficial to identify the location of the element.</li> </ul> <p> <strong>Syntax</strong> </p> <pre> from array import * arrayName = array(typecode, [data_to_be_initialized]) </pre> <p> <strong>Example</strong> </p> <pre> import array as arry n = arry.array(&apos;i&apos;, [20, 40, 60, 80]) print(&apos;First array value:&apos;, n[0]) print(&apos;Second array value:&apos;, n[1]) print(&apos;Second last array value:&apos;, n[-1]) </pre> <p> <strong>Output</strong> </p> <pre> First array value: 20 Second array value: 40 Second last array value: 80 </pre> <hr></3></pre></arr.length;></pre></arr.length;i++)></pre></4;></pre></4;></pre></4;>

2. لغة C++

في لغة C++، يحتاج المستخدم إلى تحديد نوع العنصر والطول الإجمالي للصفيف.

بناء جملة للإعلان عن المصفوفة

التطبيع في قاعدة البيانات
 type arrName [ arrSize ]; 

بناء الجملة لتهيئة الصفيف

 int arr[] = { 1, 2, 3, 4, 5 } 

مثال

 #include using namespace std; int main () { // declaring an array with 4 rows. int arr[4] = {1,7,50,6}; // traversing the output for each array value for ( int i = 0; i <4; i++ ) { cout << \'array at position[\' i \']: \'; arr[i]<< endl; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> array at position[0]: 1 array at position[1]: 7 array at position[2]: 50 array at position[3]: 6 </pre> <h3>Multidimensional Array</h3> <p>C++ language also enables the Multidimensional arrays.</p> <p> <strong>Syntax for initializing 2D array</strong> </p> <pre> int a[4][3] = { {0, 1, 2} , /* storing data in array for row indexed by 0 */ {42, 25, 62} , /* storing data in array for row indexed by 1 */ {18, 90, 15}, /* storing data in array for row indexed by 2 */ {21, 19, 25}, /* storing data in array for row indexed by 3 */ }; </pre> <p> <strong>Example</strong> </p> <pre> #include using namespace std; int main () { // declaring an array with 4 rows and 2 columns. int arr[4][2] = { {1,0}, {0,2}, {2,3}, {5,6}}; // traversing the output for each array value for ( int i = 0; i <4; i++ ) for ( int j="0;" < 2; j++ { cout << \'array at position[\' i \'][\' \']: \'; arr[i][j]<< endl; } return 0; pre> <p> <strong>Output</strong> </p> <pre> array at position[0][0]: 1 array at position[0][1]: 0 array at position[1][0]: 0 array at position[1][1]: 2 array at position[2][0]: 2 array at position[2][1]: 3 array at position[3][0]: 5 array at position[3][1]: 6 </pre> <h2>3. Java</h2> <p>In Java language, Arrays work differently than what they used to do in C or C++ language.</p> <h3>One-Dimensional Arrays:</h3> <p>To declare an array, the user needs to have two primary components: the type and the array&apos;s name.</p> <p>The &apos;Type&apos; refers to the elementary type of a specific array. It determines the data type of all elements that are included in the array. It comprises the array of primitive data types, unlike integers, char, float, double, etc., or it could include the user-defined data types (objects of a class) as well. Therefore, the element type for the array concludes what kind of data the array will contain.</p> <p> <strong>Syntax</strong> </p> <pre> type arr_var_name[]; OR type[] arr_var_name; OR var-name = new type [size]; </pre> <p> <strong>Store values in one-dimensional array</strong> </p> <p>Assigning values to an element in an array is similar to assigning values to scalar variables.</p> <pre> Array [index]= initializers; arr[1]= 50 arr[2]= 20 </pre> <h4>NOTE: It the array element is not assigned any value, by default it has a Null (empty) value.</h4> <p> <strong>Example</strong> </p> <pre> //Java Program to demonstrate how to initialize, store and display //values in one-dimensional arrays. class Testarray{ public static void main(String args[]){ int arr[]=new int[5];//declaration and instantiation arr[0]=20;//initialization of the array arr[1]=40; arr[2]=60; arr[3]=80; arr[4]=100; //traversing array to print the array values for(int i=0;i <arr.length;i++) system.out.println(arr[i]); }} < pre> <p> <strong>Output</strong> </p> <pre> 20 40 60 80 100 </pre> <h3>Arrays of Objects</h3> <p>An array of objects is constructed in the same way as an array of primitive type data elements.</p> <p> <strong>Example</strong> </p> <pre> // Java program to create, store and display values for one-dimensional // array of objects class Employee { public int id_no; public String name; Employee(int id_no, String name) { this.id_no = id_no; this.name = name; } } // The Elements of the array are objects of a class Employee. public class Array_Objects_Example { public static void main (String[] args) { // declares an Array of integers. Employee[] arr; // assigning space for 5 objects of type Employee. arr = new Employee[5]; // storing the value for the first element of the array arr[0] = new Employee(111,&apos;Varun&apos;); // storing the value for the second elements of the array arr[1] = new Employee(121,&apos;Sukla&apos;); // so on... arr[2] = new Employee(131,&apos;Virat&apos;); arr[3] = new Employee(141,&apos;Anuskha&apos;); arr[4] = new Employee(151,&apos;Mohit&apos;); // accessing the elements of the specified array for (int i = 0; i <arr.length; i++) system.out.println(\'element at \' + i : arr[i].id_no +\' \'+ arr[i].name); } < pre> <p> <strong>Output</strong> </p> <pre> Element at 0 : 111 Varun Element at 1 : 121 Sukla Element at 2 : 131 Virat Element at 3 : 141 Anuskha Element at 4 : 151 Mohit </pre> <h3>Multidimensional Arrays</h3> <p>Multidimensional arrays are termed &apos;arrays of arrays&apos; as they can hold each element of an array with the reference of another array. These are also known as Jagged Arrays. A multidimensional array is constructed by adding a set of square brackets ([]) per dimension.</p> <p> <strong>Syntax</strong> </p> <pre> int[][] intArray = new int[10][20]; //a 2D array or matrix int[][][] intArray = new int[10][20][10]; //a 3D array </pre> <p> <strong>Example to store values in a Multidimensional Array</strong> </p> <pre> arry[0][0]=10; arry[0][1]=20; arry[0][2]=30; arry[1][0]=40; arry[1][1]=50; arry[1][3]=60; arry[2][1]=70; arry[2][2]=80; arry[2][3]=90; </pre> <p> <strong>Example of Multidimensional Array</strong> </p> <pre> class multiDimensional_Example { public static void main(String args[]) { // declaring and storing data in 2-Dimensional array int arry[][] = { {12,17,19},{32,62,12},{37,34,32} }; // traversing and printing the 2-Dimensional array for (int i=0; i<3 3 ; i++) { for (int j="0;" < j++) system.out.print(arry[i][j] + \' \'); system.out.println(); } pre> <p> <strong>Output</strong> </p> <pre> 12 17 19 32 62 12 37 34 32 </pre> <h2>4. PHP</h2> <p>PHP array is an ordered map (holds elements on the base of the key-value). It is utilized to hold multiple values of a similar data type in a single variable.</p> <p>PHP contains 3 kinds of array that are as follows:</p> <ol class="points"> <li>Indexed Array</li> <li>Associative Array</li> <li>Multidimensional Array</li> </ol> <h3>1. Indexed Array</h3> <p>PHP index is described by an integer number that begins with 0 (default value). The PHP array can store any data type, such as numbers, characters, strings, and objects. All PHP array data are allocated an index number by default.</p> <p> <strong>Syntax to store values</strong> </p> <pre> $Colour =array(&apos;Red&apos;, &apos;White&apos;, &apos;Black&apos;, &apos;Yellow&apos;); </pre> <p> <strong>Or</strong> </p> <pre> $Colour[0]=&apos;Red &apos;; $Colour[1]=&apos;White&apos;; $Colour[2]=&apos;Black&apos;; $Colour[3]=&apos;Yellow&apos;; </pre> <p> <strong>Example</strong> </p> <pre> </pre> <p> <strong>Output</strong> </p> <pre> Colours are: Red, White, Black, Yellow </pre> <h3>2. Associative Array</h3> <p>In PHP, the user can associate any specific name with each array elements using the &apos;=&gt;&apos; symbol.</p> <p> <strong>Syntax</strong> </p> <pre> $marks =array(&apos;Reema&apos;=&gt;&apos;95&apos;,&apos;John&apos;=&gt;&apos;45&apos;,&apos;Rahul&apos;=&gt;&apos;20&apos;); </pre> <p> <strong>Or</strong> </p> <pre> $marks[&apos;Reema &apos;]=&apos;95&apos;; $marks[&apos;John&apos;]=&apos;45&apos;; $marks [&apos;Rahul &apos;]=&apos;20&apos;; </pre> <p> <strong>Example</strong> </p> <pre> <?php $marks[\'Reema \']=\'95\'; $marks[\'John\']=\'45\'; $marks [\'Rahul \']=\'20\'; echo \'Reema\'s Marks: \'.$marks [\'Reema \'].\' <br/>&apos;; echo &apos; John&apos;s Marks: &apos;.$marks[&apos;John&apos;].&apos; <br>&apos;; echo &apos; Rahul&apos;s Marks: &apos;.$marks [&apos;Rahul &apos;].&apos; <br>&apos;; ?&gt; </pre> <p> <strong>Output</strong> </p> <pre> Reema&apos;s Marks: 95 John&apos;s Marks: 45 Rahul&apos;s Marks: 20 </pre> <h3>3. Multidimensional Array</h3> <p>Multidimensional arrays in PHP are also termed as &apos;array of arrays&apos;. It enabled the user to store array data in a tabular format. PHP multidimensional array can be expressed in the form of a matrix which is denoted by row * column.</p> <p> <strong>Syntax</strong> </p> <pre> $emp = array ( array (1,&apos;Reema&apos;,95), array(2,&apos;john&apos;,45), array(3,&apos;rahul&apos;,20) ); </pre> <p> <strong>Example</strong> </p> <pre> <?php $emp = array ( array (1,\'Reema\',95), array(2,\'john\',45), array(3,\'rahul\',20) ); for ($row = 0; $row < 3; $row++) { for ($col = 0; $col < 3; $col++) { echo $emp[$row][$col].\' \'; } echo \' <br/>&apos;; } ?&gt; </pre> <p> <strong>Output</strong> </p> <pre> 1 Reema 95 2 john 45 3 rahul 20 </pre> <h2>5. Python</h2> <p>Python uses a module named &apos;Array&apos; to handle all the functions of Arrays in Python. It is helpful when the user wants to manipulate only particular data values. Given below are the keywords that are important to learn the concept of an array in Python:</p> <ul> <li>Element - Any data stored in an array is known an element.</li> <li>Index - Whenever an array stores any data, it has some numerical location known as index that is beneficial to identify the location of the element.</li> </ul> <p> <strong>Syntax</strong> </p> <pre> from array import * arrayName = array(typecode, [data_to_be_initialized]) </pre> <p> <strong>Example</strong> </p> <pre> import array as arry n = arry.array(&apos;i&apos;, [20, 40, 60, 80]) print(&apos;First array value:&apos;, n[0]) print(&apos;Second array value:&apos;, n[1]) print(&apos;Second last array value:&apos;, n[-1]) </pre> <p> <strong>Output</strong> </p> <pre> First array value: 20 Second array value: 40 Second last array value: 80 </pre> <hr></3></pre></arr.length;></pre></arr.length;i++)></pre></4;></pre></4;>

مصفوفة متعددة الأبعاد

تتيح لغة C++ أيضًا المصفوفات متعددة الأبعاد.

بناء الجملة لتهيئة مجموعة ثنائية الأبعاد

 int a[4][3] = { {0, 1, 2} , /* storing data in array for row indexed by 0 */ {42, 25, 62} , /* storing data in array for row indexed by 1 */ {18, 90, 15}, /* storing data in array for row indexed by 2 */ {21, 19, 25}, /* storing data in array for row indexed by 3 */ }; 

مثال

 #include using namespace std; int main () { // declaring an array with 4 rows and 2 columns. int arr[4][2] = { {1,0}, {0,2}, {2,3}, {5,6}}; // traversing the output for each array value for ( int i = 0; i <4; i++ ) for ( int j="0;" < 2; j++ { cout << \'array at position[\' i \'][\' \']: \'; arr[i][j]<< endl; } return 0; pre> <p> <strong>Output</strong> </p> <pre> array at position[0][0]: 1 array at position[0][1]: 0 array at position[1][0]: 0 array at position[1][1]: 2 array at position[2][0]: 2 array at position[2][1]: 3 array at position[3][0]: 5 array at position[3][1]: 6 </pre> <h2>3. Java</h2> <p>In Java language, Arrays work differently than what they used to do in C or C++ language.</p> <h3>One-Dimensional Arrays:</h3> <p>To declare an array, the user needs to have two primary components: the type and the array&apos;s name.</p> <p>The &apos;Type&apos; refers to the elementary type of a specific array. It determines the data type of all elements that are included in the array. It comprises the array of primitive data types, unlike integers, char, float, double, etc., or it could include the user-defined data types (objects of a class) as well. Therefore, the element type for the array concludes what kind of data the array will contain.</p> <p> <strong>Syntax</strong> </p> <pre> type arr_var_name[]; OR type[] arr_var_name; OR var-name = new type [size]; </pre> <p> <strong>Store values in one-dimensional array</strong> </p> <p>Assigning values to an element in an array is similar to assigning values to scalar variables.</p> <pre> Array [index]= initializers; arr[1]= 50 arr[2]= 20 </pre> <h4>NOTE: It the array element is not assigned any value, by default it has a Null (empty) value.</h4> <p> <strong>Example</strong> </p> <pre> //Java Program to demonstrate how to initialize, store and display //values in one-dimensional arrays. class Testarray{ public static void main(String args[]){ int arr[]=new int[5];//declaration and instantiation arr[0]=20;//initialization of the array arr[1]=40; arr[2]=60; arr[3]=80; arr[4]=100; //traversing array to print the array values for(int i=0;i <arr.length;i++) system.out.println(arr[i]); }} < pre> <p> <strong>Output</strong> </p> <pre> 20 40 60 80 100 </pre> <h3>Arrays of Objects</h3> <p>An array of objects is constructed in the same way as an array of primitive type data elements.</p> <p> <strong>Example</strong> </p> <pre> // Java program to create, store and display values for one-dimensional // array of objects class Employee { public int id_no; public String name; Employee(int id_no, String name) { this.id_no = id_no; this.name = name; } } // The Elements of the array are objects of a class Employee. public class Array_Objects_Example { public static void main (String[] args) { // declares an Array of integers. Employee[] arr; // assigning space for 5 objects of type Employee. arr = new Employee[5]; // storing the value for the first element of the array arr[0] = new Employee(111,&apos;Varun&apos;); // storing the value for the second elements of the array arr[1] = new Employee(121,&apos;Sukla&apos;); // so on... arr[2] = new Employee(131,&apos;Virat&apos;); arr[3] = new Employee(141,&apos;Anuskha&apos;); arr[4] = new Employee(151,&apos;Mohit&apos;); // accessing the elements of the specified array for (int i = 0; i <arr.length; i++) system.out.println(\'element at \' + i : arr[i].id_no +\' \'+ arr[i].name); } < pre> <p> <strong>Output</strong> </p> <pre> Element at 0 : 111 Varun Element at 1 : 121 Sukla Element at 2 : 131 Virat Element at 3 : 141 Anuskha Element at 4 : 151 Mohit </pre> <h3>Multidimensional Arrays</h3> <p>Multidimensional arrays are termed &apos;arrays of arrays&apos; as they can hold each element of an array with the reference of another array. These are also known as Jagged Arrays. A multidimensional array is constructed by adding a set of square brackets ([]) per dimension.</p> <p> <strong>Syntax</strong> </p> <pre> int[][] intArray = new int[10][20]; //a 2D array or matrix int[][][] intArray = new int[10][20][10]; //a 3D array </pre> <p> <strong>Example to store values in a Multidimensional Array</strong> </p> <pre> arry[0][0]=10; arry[0][1]=20; arry[0][2]=30; arry[1][0]=40; arry[1][1]=50; arry[1][3]=60; arry[2][1]=70; arry[2][2]=80; arry[2][3]=90; </pre> <p> <strong>Example of Multidimensional Array</strong> </p> <pre> class multiDimensional_Example { public static void main(String args[]) { // declaring and storing data in 2-Dimensional array int arry[][] = { {12,17,19},{32,62,12},{37,34,32} }; // traversing and printing the 2-Dimensional array for (int i=0; i<3 3 ; i++) { for (int j="0;" < j++) system.out.print(arry[i][j] + \' \'); system.out.println(); } pre> <p> <strong>Output</strong> </p> <pre> 12 17 19 32 62 12 37 34 32 </pre> <h2>4. PHP</h2> <p>PHP array is an ordered map (holds elements on the base of the key-value). It is utilized to hold multiple values of a similar data type in a single variable.</p> <p>PHP contains 3 kinds of array that are as follows:</p> <ol class="points"> <li>Indexed Array</li> <li>Associative Array</li> <li>Multidimensional Array</li> </ol> <h3>1. Indexed Array</h3> <p>PHP index is described by an integer number that begins with 0 (default value). The PHP array can store any data type, such as numbers, characters, strings, and objects. All PHP array data are allocated an index number by default.</p> <p> <strong>Syntax to store values</strong> </p> <pre> $Colour =array(&apos;Red&apos;, &apos;White&apos;, &apos;Black&apos;, &apos;Yellow&apos;); </pre> <p> <strong>Or</strong> </p> <pre> $Colour[0]=&apos;Red &apos;; $Colour[1]=&apos;White&apos;; $Colour[2]=&apos;Black&apos;; $Colour[3]=&apos;Yellow&apos;; </pre> <p> <strong>Example</strong> </p> <pre> </pre> <p> <strong>Output</strong> </p> <pre> Colours are: Red, White, Black, Yellow </pre> <h3>2. Associative Array</h3> <p>In PHP, the user can associate any specific name with each array elements using the &apos;=&gt;&apos; symbol.</p> <p> <strong>Syntax</strong> </p> <pre> $marks =array(&apos;Reema&apos;=&gt;&apos;95&apos;,&apos;John&apos;=&gt;&apos;45&apos;,&apos;Rahul&apos;=&gt;&apos;20&apos;); </pre> <p> <strong>Or</strong> </p> <pre> $marks[&apos;Reema &apos;]=&apos;95&apos;; $marks[&apos;John&apos;]=&apos;45&apos;; $marks [&apos;Rahul &apos;]=&apos;20&apos;; </pre> <p> <strong>Example</strong> </p> <pre> <?php $marks[\'Reema \']=\'95\'; $marks[\'John\']=\'45\'; $marks [\'Rahul \']=\'20\'; echo \'Reema\'s Marks: \'.$marks [\'Reema \'].\' <br/>&apos;; echo &apos; John&apos;s Marks: &apos;.$marks[&apos;John&apos;].&apos; <br>&apos;; echo &apos; Rahul&apos;s Marks: &apos;.$marks [&apos;Rahul &apos;].&apos; <br>&apos;; ?&gt; </pre> <p> <strong>Output</strong> </p> <pre> Reema&apos;s Marks: 95 John&apos;s Marks: 45 Rahul&apos;s Marks: 20 </pre> <h3>3. Multidimensional Array</h3> <p>Multidimensional arrays in PHP are also termed as &apos;array of arrays&apos;. It enabled the user to store array data in a tabular format. PHP multidimensional array can be expressed in the form of a matrix which is denoted by row * column.</p> <p> <strong>Syntax</strong> </p> <pre> $emp = array ( array (1,&apos;Reema&apos;,95), array(2,&apos;john&apos;,45), array(3,&apos;rahul&apos;,20) ); </pre> <p> <strong>Example</strong> </p> <pre> <?php $emp = array ( array (1,\'Reema\',95), array(2,\'john\',45), array(3,\'rahul\',20) ); for ($row = 0; $row < 3; $row++) { for ($col = 0; $col < 3; $col++) { echo $emp[$row][$col].\' \'; } echo \' <br/>&apos;; } ?&gt; </pre> <p> <strong>Output</strong> </p> <pre> 1 Reema 95 2 john 45 3 rahul 20 </pre> <h2>5. Python</h2> <p>Python uses a module named &apos;Array&apos; to handle all the functions of Arrays in Python. It is helpful when the user wants to manipulate only particular data values. Given below are the keywords that are important to learn the concept of an array in Python:</p> <ul> <li>Element - Any data stored in an array is known an element.</li> <li>Index - Whenever an array stores any data, it has some numerical location known as index that is beneficial to identify the location of the element.</li> </ul> <p> <strong>Syntax</strong> </p> <pre> from array import * arrayName = array(typecode, [data_to_be_initialized]) </pre> <p> <strong>Example</strong> </p> <pre> import array as arry n = arry.array(&apos;i&apos;, [20, 40, 60, 80]) print(&apos;First array value:&apos;, n[0]) print(&apos;Second array value:&apos;, n[1]) print(&apos;Second last array value:&apos;, n[-1]) </pre> <p> <strong>Output</strong> </p> <pre> First array value: 20 Second array value: 40 Second last array value: 80 </pre> <hr></3></pre></arr.length;></pre></arr.length;i++)></pre></4;>

3. جافا

في لغة Java، تعمل المصفوفات بشكل مختلف عما كانت تفعله في لغة C أو C++.

المصفوفات أحادية البعد:

للإعلان عن مصفوفة، يحتاج المستخدم إلى مكونين أساسيين: النوع واسم المصفوفة.

يشير 'النوع' إلى النوع الأولي لمصفوفة محددة. فهو يحدد نوع البيانات لجميع العناصر المضمنة في المصفوفة. وهي تشتمل على مجموعة من أنواع البيانات البدائية، على عكس الأعداد الصحيحة، وchar، وfloat، وdouble، وما إلى ذلك، أو يمكن أن تشمل أنواع البيانات المحددة من قبل المستخدم (كائنات فئة) أيضًا. لذلك، يحدد نوع عنصر المصفوفة نوع البيانات التي سيحتوي عليها المصفوفة.

بناء الجملة

 type arr_var_name[]; OR type[] arr_var_name; OR var-name = new type [size]; 

تخزين القيم في صفيف أحادي البعد

يشبه تعيين القيم لعنصر في المصفوفة تعيين القيم للمتغيرات العددية.

 Array [index]= initializers; arr[1]= 50 arr[2]= 20 

ملاحظة: لم يتم تعيين أي قيمة لعنصر الصفيف، بشكل افتراضي له قيمة فارغة (فارغة).

مثال

أميشا باتيل
 //Java Program to demonstrate how to initialize, store and display //values in one-dimensional arrays. class Testarray{ public static void main(String args[]){ int arr[]=new int[5];//declaration and instantiation arr[0]=20;//initialization of the array arr[1]=40; arr[2]=60; arr[3]=80; arr[4]=100; //traversing array to print the array values for(int i=0;i <arr.length;i++) system.out.println(arr[i]); }} < pre> <p> <strong>Output</strong> </p> <pre> 20 40 60 80 100 </pre> <h3>Arrays of Objects</h3> <p>An array of objects is constructed in the same way as an array of primitive type data elements.</p> <p> <strong>Example</strong> </p> <pre> // Java program to create, store and display values for one-dimensional // array of objects class Employee { public int id_no; public String name; Employee(int id_no, String name) { this.id_no = id_no; this.name = name; } } // The Elements of the array are objects of a class Employee. public class Array_Objects_Example { public static void main (String[] args) { // declares an Array of integers. Employee[] arr; // assigning space for 5 objects of type Employee. arr = new Employee[5]; // storing the value for the first element of the array arr[0] = new Employee(111,&apos;Varun&apos;); // storing the value for the second elements of the array arr[1] = new Employee(121,&apos;Sukla&apos;); // so on... arr[2] = new Employee(131,&apos;Virat&apos;); arr[3] = new Employee(141,&apos;Anuskha&apos;); arr[4] = new Employee(151,&apos;Mohit&apos;); // accessing the elements of the specified array for (int i = 0; i <arr.length; i++) system.out.println(\'element at \' + i : arr[i].id_no +\' \'+ arr[i].name); } < pre> <p> <strong>Output</strong> </p> <pre> Element at 0 : 111 Varun Element at 1 : 121 Sukla Element at 2 : 131 Virat Element at 3 : 141 Anuskha Element at 4 : 151 Mohit </pre> <h3>Multidimensional Arrays</h3> <p>Multidimensional arrays are termed &apos;arrays of arrays&apos; as they can hold each element of an array with the reference of another array. These are also known as Jagged Arrays. A multidimensional array is constructed by adding a set of square brackets ([]) per dimension.</p> <p> <strong>Syntax</strong> </p> <pre> int[][] intArray = new int[10][20]; //a 2D array or matrix int[][][] intArray = new int[10][20][10]; //a 3D array </pre> <p> <strong>Example to store values in a Multidimensional Array</strong> </p> <pre> arry[0][0]=10; arry[0][1]=20; arry[0][2]=30; arry[1][0]=40; arry[1][1]=50; arry[1][3]=60; arry[2][1]=70; arry[2][2]=80; arry[2][3]=90; </pre> <p> <strong>Example of Multidimensional Array</strong> </p> <pre> class multiDimensional_Example { public static void main(String args[]) { // declaring and storing data in 2-Dimensional array int arry[][] = { {12,17,19},{32,62,12},{37,34,32} }; // traversing and printing the 2-Dimensional array for (int i=0; i<3 3 ; i++) { for (int j="0;" < j++) system.out.print(arry[i][j] + \' \'); system.out.println(); } pre> <p> <strong>Output</strong> </p> <pre> 12 17 19 32 62 12 37 34 32 </pre> <h2>4. PHP</h2> <p>PHP array is an ordered map (holds elements on the base of the key-value). It is utilized to hold multiple values of a similar data type in a single variable.</p> <p>PHP contains 3 kinds of array that are as follows:</p> <ol class="points"> <li>Indexed Array</li> <li>Associative Array</li> <li>Multidimensional Array</li> </ol> <h3>1. Indexed Array</h3> <p>PHP index is described by an integer number that begins with 0 (default value). The PHP array can store any data type, such as numbers, characters, strings, and objects. All PHP array data are allocated an index number by default.</p> <p> <strong>Syntax to store values</strong> </p> <pre> $Colour =array(&apos;Red&apos;, &apos;White&apos;, &apos;Black&apos;, &apos;Yellow&apos;); </pre> <p> <strong>Or</strong> </p> <pre> $Colour[0]=&apos;Red &apos;; $Colour[1]=&apos;White&apos;; $Colour[2]=&apos;Black&apos;; $Colour[3]=&apos;Yellow&apos;; </pre> <p> <strong>Example</strong> </p> <pre> </pre> <p> <strong>Output</strong> </p> <pre> Colours are: Red, White, Black, Yellow </pre> <h3>2. Associative Array</h3> <p>In PHP, the user can associate any specific name with each array elements using the &apos;=&gt;&apos; symbol.</p> <p> <strong>Syntax</strong> </p> <pre> $marks =array(&apos;Reema&apos;=&gt;&apos;95&apos;,&apos;John&apos;=&gt;&apos;45&apos;,&apos;Rahul&apos;=&gt;&apos;20&apos;); </pre> <p> <strong>Or</strong> </p> <pre> $marks[&apos;Reema &apos;]=&apos;95&apos;; $marks[&apos;John&apos;]=&apos;45&apos;; $marks [&apos;Rahul &apos;]=&apos;20&apos;; </pre> <p> <strong>Example</strong> </p> <pre> <?php $marks[\'Reema \']=\'95\'; $marks[\'John\']=\'45\'; $marks [\'Rahul \']=\'20\'; echo \'Reema\'s Marks: \'.$marks [\'Reema \'].\' <br/>&apos;; echo &apos; John&apos;s Marks: &apos;.$marks[&apos;John&apos;].&apos; <br>&apos;; echo &apos; Rahul&apos;s Marks: &apos;.$marks [&apos;Rahul &apos;].&apos; <br>&apos;; ?&gt; </pre> <p> <strong>Output</strong> </p> <pre> Reema&apos;s Marks: 95 John&apos;s Marks: 45 Rahul&apos;s Marks: 20 </pre> <h3>3. Multidimensional Array</h3> <p>Multidimensional arrays in PHP are also termed as &apos;array of arrays&apos;. It enabled the user to store array data in a tabular format. PHP multidimensional array can be expressed in the form of a matrix which is denoted by row * column.</p> <p> <strong>Syntax</strong> </p> <pre> $emp = array ( array (1,&apos;Reema&apos;,95), array(2,&apos;john&apos;,45), array(3,&apos;rahul&apos;,20) ); </pre> <p> <strong>Example</strong> </p> <pre> <?php $emp = array ( array (1,\'Reema\',95), array(2,\'john\',45), array(3,\'rahul\',20) ); for ($row = 0; $row < 3; $row++) { for ($col = 0; $col < 3; $col++) { echo $emp[$row][$col].\' \'; } echo \' <br/>&apos;; } ?&gt; </pre> <p> <strong>Output</strong> </p> <pre> 1 Reema 95 2 john 45 3 rahul 20 </pre> <h2>5. Python</h2> <p>Python uses a module named &apos;Array&apos; to handle all the functions of Arrays in Python. It is helpful when the user wants to manipulate only particular data values. Given below are the keywords that are important to learn the concept of an array in Python:</p> <ul> <li>Element - Any data stored in an array is known an element.</li> <li>Index - Whenever an array stores any data, it has some numerical location known as index that is beneficial to identify the location of the element.</li> </ul> <p> <strong>Syntax</strong> </p> <pre> from array import * arrayName = array(typecode, [data_to_be_initialized]) </pre> <p> <strong>Example</strong> </p> <pre> import array as arry n = arry.array(&apos;i&apos;, [20, 40, 60, 80]) print(&apos;First array value:&apos;, n[0]) print(&apos;Second array value:&apos;, n[1]) print(&apos;Second last array value:&apos;, n[-1]) </pre> <p> <strong>Output</strong> </p> <pre> First array value: 20 Second array value: 40 Second last array value: 80 </pre> <hr></3></pre></arr.length;></pre></arr.length;i++)>

مصفوفات الكائنات

يتم إنشاء مصفوفة من الكائنات بنفس طريقة إنشاء مصفوفة من عناصر البيانات من النوع البدائي.

مثال

 // Java program to create, store and display values for one-dimensional // array of objects class Employee { public int id_no; public String name; Employee(int id_no, String name) { this.id_no = id_no; this.name = name; } } // The Elements of the array are objects of a class Employee. public class Array_Objects_Example { public static void main (String[] args) { // declares an Array of integers. Employee[] arr; // assigning space for 5 objects of type Employee. arr = new Employee[5]; // storing the value for the first element of the array arr[0] = new Employee(111,&apos;Varun&apos;); // storing the value for the second elements of the array arr[1] = new Employee(121,&apos;Sukla&apos;); // so on... arr[2] = new Employee(131,&apos;Virat&apos;); arr[3] = new Employee(141,&apos;Anuskha&apos;); arr[4] = new Employee(151,&apos;Mohit&apos;); // accessing the elements of the specified array for (int i = 0; i <arr.length; i++) system.out.println(\\'element at \\' + i : arr[i].id_no +\\' \\'+ arr[i].name); } < pre> <p> <strong>Output</strong> </p> <pre> Element at 0 : 111 Varun Element at 1 : 121 Sukla Element at 2 : 131 Virat Element at 3 : 141 Anuskha Element at 4 : 151 Mohit </pre> <h3>Multidimensional Arrays</h3> <p>Multidimensional arrays are termed &apos;arrays of arrays&apos; as they can hold each element of an array with the reference of another array. These are also known as Jagged Arrays. A multidimensional array is constructed by adding a set of square brackets ([]) per dimension.</p> <p> <strong>Syntax</strong> </p> <pre> int[][] intArray = new int[10][20]; //a 2D array or matrix int[][][] intArray = new int[10][20][10]; //a 3D array </pre> <p> <strong>Example to store values in a Multidimensional Array</strong> </p> <pre> arry[0][0]=10; arry[0][1]=20; arry[0][2]=30; arry[1][0]=40; arry[1][1]=50; arry[1][3]=60; arry[2][1]=70; arry[2][2]=80; arry[2][3]=90; </pre> <p> <strong>Example of Multidimensional Array</strong> </p> <pre> class multiDimensional_Example { public static void main(String args[]) { // declaring and storing data in 2-Dimensional array int arry[][] = { {12,17,19},{32,62,12},{37,34,32} }; // traversing and printing the 2-Dimensional array for (int i=0; i<3 3 ; i++) { for (int j="0;" < j++) system.out.print(arry[i][j] + \\' \\'); system.out.println(); } pre> <p> <strong>Output</strong> </p> <pre> 12 17 19 32 62 12 37 34 32 </pre> <h2>4. PHP</h2> <p>PHP array is an ordered map (holds elements on the base of the key-value). It is utilized to hold multiple values of a similar data type in a single variable.</p> <p>PHP contains 3 kinds of array that are as follows:</p> <ol class="points"> <li>Indexed Array</li> <li>Associative Array</li> <li>Multidimensional Array</li> </ol> <h3>1. Indexed Array</h3> <p>PHP index is described by an integer number that begins with 0 (default value). The PHP array can store any data type, such as numbers, characters, strings, and objects. All PHP array data are allocated an index number by default.</p> <p> <strong>Syntax to store values</strong> </p> <pre> $Colour =array(&apos;Red&apos;, &apos;White&apos;, &apos;Black&apos;, &apos;Yellow&apos;); </pre> <p> <strong>Or</strong> </p> <pre> $Colour[0]=&apos;Red &apos;; $Colour[1]=&apos;White&apos;; $Colour[2]=&apos;Black&apos;; $Colour[3]=&apos;Yellow&apos;; </pre> <p> <strong>Example</strong> </p> <pre> </pre> <p> <strong>Output</strong> </p> <pre> Colours are: Red, White, Black, Yellow </pre> <h3>2. Associative Array</h3> <p>In PHP, the user can associate any specific name with each array elements using the &apos;=&gt;&apos; symbol.</p> <p> <strong>Syntax</strong> </p> <pre> $marks =array(&apos;Reema&apos;=&gt;&apos;95&apos;,&apos;John&apos;=&gt;&apos;45&apos;,&apos;Rahul&apos;=&gt;&apos;20&apos;); </pre> <p> <strong>Or</strong> </p> <pre> $marks[&apos;Reema &apos;]=&apos;95&apos;; $marks[&apos;John&apos;]=&apos;45&apos;; $marks [&apos;Rahul &apos;]=&apos;20&apos;; </pre> <p> <strong>Example</strong> </p> <pre> <?php $marks[\\'Reema \\']=\\'95\\'; $marks[\\'John\\']=\\'45\\'; $marks [\\'Rahul \\']=\\'20\\'; echo \\'Reema\\'s Marks: \\'.$marks [\\'Reema \\'].\\' <br/>&apos;; echo &apos; John&apos;s Marks: &apos;.$marks[&apos;John&apos;].&apos; <br>&apos;; echo &apos; Rahul&apos;s Marks: &apos;.$marks [&apos;Rahul &apos;].&apos; <br>&apos;; ?&gt; </pre> <p> <strong>Output</strong> </p> <pre> Reema&apos;s Marks: 95 John&apos;s Marks: 45 Rahul&apos;s Marks: 20 </pre> <h3>3. Multidimensional Array</h3> <p>Multidimensional arrays in PHP are also termed as &apos;array of arrays&apos;. It enabled the user to store array data in a tabular format. PHP multidimensional array can be expressed in the form of a matrix which is denoted by row * column.</p> <p> <strong>Syntax</strong> </p> <pre> $emp = array ( array (1,&apos;Reema&apos;,95), array(2,&apos;john&apos;,45), array(3,&apos;rahul&apos;,20) ); </pre> <p> <strong>Example</strong> </p> <pre> <?php $emp = array ( array (1,\\'Reema\\',95), array(2,\\'john\\',45), array(3,\\'rahul\\',20) ); for ($row = 0; $row < 3; $row++) { for ($col = 0; $col < 3; $col++) { echo $emp[$row][$col].\\' \\'; } echo \\' <br/>&apos;; } ?&gt; </pre> <p> <strong>Output</strong> </p> <pre> 1 Reema 95 2 john 45 3 rahul 20 </pre> <h2>5. Python</h2> <p>Python uses a module named &apos;Array&apos; to handle all the functions of Arrays in Python. It is helpful when the user wants to manipulate only particular data values. Given below are the keywords that are important to learn the concept of an array in Python:</p> <ul> <li>Element - Any data stored in an array is known an element.</li> <li>Index - Whenever an array stores any data, it has some numerical location known as index that is beneficial to identify the location of the element.</li> </ul> <p> <strong>Syntax</strong> </p> <pre> from array import * arrayName = array(typecode, [data_to_be_initialized]) </pre> <p> <strong>Example</strong> </p> <pre> import array as arry n = arry.array(&apos;i&apos;, [20, 40, 60, 80]) print(&apos;First array value:&apos;, n[0]) print(&apos;Second array value:&apos;, n[1]) print(&apos;Second last array value:&apos;, n[-1]) </pre> <p> <strong>Output</strong> </p> <pre> First array value: 20 Second array value: 40 Second last array value: 80 </pre> <hr></3></pre></arr.length;>

مصفوفات متعددة الأبعاد

تسمى المصفوفات متعددة الأبعاد 'مصفوفات المصفوفات' لأنها يمكن أن تحتوي على كل عنصر من عناصر المصفوفة مع مرجع مصفوفة أخرى. تُعرف هذه أيضًا باسم المصفوفات الخشنة. يتم إنشاء مصفوفة متعددة الأبعاد عن طريق إضافة مجموعة من الأقواس المربعة ([]) لكل بُعد.

بناء الجملة

 int[][] intArray = new int[10][20]; //a 2D array or matrix int[][][] intArray = new int[10][20][10]; //a 3D array 

مثال لتخزين القيم في مصفوفة متعددة الأبعاد

 arry[0][0]=10; arry[0][1]=20; arry[0][2]=30; arry[1][0]=40; arry[1][1]=50; arry[1][3]=60; arry[2][1]=70; arry[2][2]=80; arry[2][3]=90; 

مثال على مصفوفة متعددة الأبعاد

 class multiDimensional_Example { public static void main(String args[]) { // declaring and storing data in 2-Dimensional array int arry[][] = { {12,17,19},{32,62,12},{37,34,32} }; // traversing and printing the 2-Dimensional array for (int i=0; i<3 3 ; i++) { for (int j="0;" < j++) system.out.print(arry[i][j] + \\' \\'); system.out.println(); } pre> <p> <strong>Output</strong> </p> <pre> 12 17 19 32 62 12 37 34 32 </pre> <h2>4. PHP</h2> <p>PHP array is an ordered map (holds elements on the base of the key-value). It is utilized to hold multiple values of a similar data type in a single variable.</p> <p>PHP contains 3 kinds of array that are as follows:</p> <ol class="points"> <li>Indexed Array</li> <li>Associative Array</li> <li>Multidimensional Array</li> </ol> <h3>1. Indexed Array</h3> <p>PHP index is described by an integer number that begins with 0 (default value). The PHP array can store any data type, such as numbers, characters, strings, and objects. All PHP array data are allocated an index number by default.</p> <p> <strong>Syntax to store values</strong> </p> <pre> $Colour =array(&apos;Red&apos;, &apos;White&apos;, &apos;Black&apos;, &apos;Yellow&apos;); </pre> <p> <strong>Or</strong> </p> <pre> $Colour[0]=&apos;Red &apos;; $Colour[1]=&apos;White&apos;; $Colour[2]=&apos;Black&apos;; $Colour[3]=&apos;Yellow&apos;; </pre> <p> <strong>Example</strong> </p> <pre> </pre> <p> <strong>Output</strong> </p> <pre> Colours are: Red, White, Black, Yellow </pre> <h3>2. Associative Array</h3> <p>In PHP, the user can associate any specific name with each array elements using the &apos;=&gt;&apos; symbol.</p> <p> <strong>Syntax</strong> </p> <pre> $marks =array(&apos;Reema&apos;=&gt;&apos;95&apos;,&apos;John&apos;=&gt;&apos;45&apos;,&apos;Rahul&apos;=&gt;&apos;20&apos;); </pre> <p> <strong>Or</strong> </p> <pre> $marks[&apos;Reema &apos;]=&apos;95&apos;; $marks[&apos;John&apos;]=&apos;45&apos;; $marks [&apos;Rahul &apos;]=&apos;20&apos;; </pre> <p> <strong>Example</strong> </p> <pre> <?php $marks[\\'Reema \\']=\\'95\\'; $marks[\\'John\\']=\\'45\\'; $marks [\\'Rahul \\']=\\'20\\'; echo \\'Reema\\'s Marks: \\'.$marks [\\'Reema \\'].\\' <br/>&apos;; echo &apos; John&apos;s Marks: &apos;.$marks[&apos;John&apos;].&apos; <br>&apos;; echo &apos; Rahul&apos;s Marks: &apos;.$marks [&apos;Rahul &apos;].&apos; <br>&apos;; ?&gt; </pre> <p> <strong>Output</strong> </p> <pre> Reema&apos;s Marks: 95 John&apos;s Marks: 45 Rahul&apos;s Marks: 20 </pre> <h3>3. Multidimensional Array</h3> <p>Multidimensional arrays in PHP are also termed as &apos;array of arrays&apos;. It enabled the user to store array data in a tabular format. PHP multidimensional array can be expressed in the form of a matrix which is denoted by row * column.</p> <p> <strong>Syntax</strong> </p> <pre> $emp = array ( array (1,&apos;Reema&apos;,95), array(2,&apos;john&apos;,45), array(3,&apos;rahul&apos;,20) ); </pre> <p> <strong>Example</strong> </p> <pre> <?php $emp = array ( array (1,\\'Reema\\',95), array(2,\\'john\\',45), array(3,\\'rahul\\',20) ); for ($row = 0; $row < 3; $row++) { for ($col = 0; $col < 3; $col++) { echo $emp[$row][$col].\\' \\'; } echo \\' <br/>&apos;; } ?&gt; </pre> <p> <strong>Output</strong> </p> <pre> 1 Reema 95 2 john 45 3 rahul 20 </pre> <h2>5. Python</h2> <p>Python uses a module named &apos;Array&apos; to handle all the functions of Arrays in Python. It is helpful when the user wants to manipulate only particular data values. Given below are the keywords that are important to learn the concept of an array in Python:</p> <ul> <li>Element - Any data stored in an array is known an element.</li> <li>Index - Whenever an array stores any data, it has some numerical location known as index that is beneficial to identify the location of the element.</li> </ul> <p> <strong>Syntax</strong> </p> <pre> from array import * arrayName = array(typecode, [data_to_be_initialized]) </pre> <p> <strong>Example</strong> </p> <pre> import array as arry n = arry.array(&apos;i&apos;, [20, 40, 60, 80]) print(&apos;First array value:&apos;, n[0]) print(&apos;Second array value:&apos;, n[1]) print(&apos;Second last array value:&apos;, n[-1]) </pre> <p> <strong>Output</strong> </p> <pre> First array value: 20 Second array value: 40 Second last array value: 80 </pre> <hr></3>

4. بي إتش بي

مصفوفة PHP عبارة عن خريطة مرتبة (تحتوي على عناصر على أساس قيمة المفتاح). يتم استخدامه للاحتفاظ بقيم متعددة من نوع بيانات مماثل في متغير واحد.

تحتوي لغة PHP على ثلاثة أنواع من المصفوفات وهي كما يلي:

  1. المصفوفة المفهرسة
  2. مصفوفة متصلة
  3. مصفوفة متعددة الأبعاد

1. المصفوفة المفهرسة

يتم وصف فهرس PHP برقم صحيح يبدأ بـ 0 (القيمة الافتراضية). يمكن لمصفوفة PHP تخزين أي نوع بيانات، مثل الأرقام والأحرف والسلاسل والكائنات. يتم تخصيص رقم فهرس لجميع بيانات مصفوفة PHP بشكل افتراضي.

بناء الجملة لتخزين القيم

 $Colour =array(&apos;Red&apos;, &apos;White&apos;, &apos;Black&apos;, &apos;Yellow&apos;); 

أو

 $Colour[0]=&apos;Red &apos;; $Colour[1]=&apos;White&apos;; $Colour[2]=&apos;Black&apos;; $Colour[3]=&apos;Yellow&apos;; 

مثال

 

انتاج |

 Colours are: Red, White, Black, Yellow 

2. الصفيف النقابي

في PHP، يمكن للمستخدم ربط أي اسم محدد بكل عناصر المصفوفة باستخدام الرمز '=>'.

بناء الجملة

 $marks =array(&apos;Reema&apos;=&gt;&apos;95&apos;,&apos;John&apos;=&gt;&apos;45&apos;,&apos;Rahul&apos;=&gt;&apos;20&apos;); 

أو

 $marks[&apos;Reema &apos;]=&apos;95&apos;; $marks[&apos;John&apos;]=&apos;45&apos;; $marks [&apos;Rahul &apos;]=&apos;20&apos;; 

مثال

فرز الإدراج في جافا
 <?php $marks[\\'Reema \\']=\\'95\\'; $marks[\\'John\\']=\\'45\\'; $marks [\\'Rahul \\']=\\'20\\'; echo \\'Reema\\'s Marks: \\'.$marks [\\'Reema \\'].\\' <br/>&apos;; echo &apos; John&apos;s Marks: &apos;.$marks[&apos;John&apos;].&apos; <br>&apos;; echo &apos; Rahul&apos;s Marks: &apos;.$marks [&apos;Rahul &apos;].&apos; <br>&apos;; ?&gt; 

انتاج |

 Reema&apos;s Marks: 95 John&apos;s Marks: 45 Rahul&apos;s Marks: 20 

3. مصفوفة متعددة الأبعاد

تُسمى المصفوفات متعددة الأبعاد في PHP أيضًا باسم 'مصفوفة المصفوفات'. لقد مكن المستخدم من تخزين بيانات المصفوفة بتنسيق جدولي. يمكن التعبير عن مصفوفة PHP متعددة الأبعاد في شكل مصفوفة يُشار إليها بالصف * العمود.

بناء الجملة

 $emp = array ( array (1,&apos;Reema&apos;,95), array(2,&apos;john&apos;,45), array(3,&apos;rahul&apos;,20) ); 

مثال

 <?php $emp = array ( array (1,\\'Reema\\',95), array(2,\\'john\\',45), array(3,\\'rahul\\',20) ); for ($row = 0; $row < 3; $row++) { for ($col = 0; $col < 3; $col++) { echo $emp[$row][$col].\\' \\'; } echo \\' <br/>&apos;; } ?&gt; 

انتاج |

 1 Reema 95 2 john 45 3 rahul 20 

5. بايثون

تستخدم Python وحدة تسمى 'Array' للتعامل مع جميع وظائف المصفوفات في Python. يكون ذلك مفيدًا عندما يريد المستخدم التعامل مع قيم بيانات معينة فقط. فيما يلي الكلمات الأساسية المهمة لتعلم مفهوم المصفوفة في بايثون:

  • العنصر - أي بيانات مخزنة في مصفوفة تُعرف بالعنصر.
  • الفهرس - عندما تقوم المصفوفة بتخزين أي بيانات، فإنها تحتوي على موقع رقمي يُعرف باسم الفهرس وهو مفيد لتحديد موقع العنصر.

بناء الجملة

 from array import * arrayName = array(typecode, [data_to_be_initialized]) 

مثال

 import array as arry n = arry.array(&apos;i&apos;, [20, 40, 60, 80]) print(&apos;First array value:&apos;, n[0]) print(&apos;Second array value:&apos;, n[1]) print(&apos;Second last array value:&apos;, n[-1]) 

انتاج |

 First array value: 20 Second array value: 40 Second last array value: 80