logo

Kako shraniti vrednost v matriko

Matrika je skupina podobnih tipov elementov, ki ima sosednjo pomnilniško lokacijo. Matrika je zbirka podobno tipiziranih spremenljivk, ki so navedene s skupnim imenom.

Ta vadnica bo na kratko odkrila, kako shraniti vrednost v matriko v pogosto uporabljenih jezikih.

1. C Jezik

Vse matrike so zvezni bloki pomnilniških lokacij. Privzeto je na najnižjem položaju matrike shranjen prvi element, na najvišjem položaju pa so shranjeni zadnji podatki. V C je matrika deklarirana tako, da se določi vrsta elementa in skupna dolžina matrike, potrebne za shranjevanje podatkov.

Sintaksa za deklaracijo matrike

 type arrayName [ arrSize ]; 

Sintaksa za inicializacijo za shranjevanje vrednosti polja

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

Primer

 #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;>

Večdimenzionalni niz v C

V jeziku C se do elementov 2D (dvodimenzionalne) matrike dostopa s pomočjo indeksov, to je številke indeksa vrstice in številke indeksa stolpca matrike.

Sintaksa za deklaracijo Array

 int val = arr[x][y]; 

Sintaksa za inicializacijo dvodimenzionalnih nizov

 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. Jezik C++

V jeziku C++ mora uporabnik določiti vrsto elementa in skupno dolžino matrike.

Sintaksa za deklariranje matrike

 type arrName [ arrSize ]; 

Sintaksa za inicializacijo matrike

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

Primer

 #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;>

Večdimenzionalni niz

Jezik C++ omogoča tudi večdimenzionalne nize.

Sintaksa za inicializacijo 2D polja

 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 */ }; 

Primer

 #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

V jeziku Java matrike delujejo drugače kot v jeziku C ali C++.

Enodimenzionalni nizi:

Za deklaracijo matrike mora imeti uporabnik dve primarni komponenti: vrsto in ime matrike.

'Tip' se nanaša na osnovni tip določene matrike. Določa podatkovni tip vseh elementov, ki so vključeni v matriko. Vsebuje niz primitivnih podatkovnih tipov, za razliko od celih števil, char, float, double itd., lahko pa vključuje tudi uporabniško definirane podatkovne tipe (objekte razreda). Zato vrsta elementa za matriko določa, kakšne podatke bo matrika vsebovala.

Sintaksa

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

Shranjevanje vrednosti v enodimenzionalnem nizu

Dodeljevanje vrednosti elementu v matriki je podobno dodeljevanju vrednosti skalarnim spremenljivkam.

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

OPOMBA: če elementu matrike ni dodeljena nobena vrednost, ima privzeto ničelno (prazno) vrednost.

Primer

imessage igre za android
 //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++)>

Nizi predmetov

Matrika objektov je zgrajena na enak način kot matrika podatkovnih elementov primitivnega tipa.

Primer

 // 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;>

Večdimenzionalni nizi

Večdimenzionalne matrike imenujemo 'matrike matrik', saj lahko vsebujejo vsak element matrike z referenco druge matrike. Ti so znani tudi kot nazobčani nizi. Večdimenzionalno polje je sestavljeno z dodajanjem niza oglatih oklepajev ([]) na dimenzijo.

Sintaksa

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

Primer shranjevanja vrednosti v večdimenzionalno matriko

 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; 

Primer večdimenzionalne matrike

 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

Matrika PHP je urejen zemljevid (vsebuje elemente na podlagi ključ-vrednosti). Uporablja se za shranjevanje več vrednosti podobne vrste podatkov v eni spremenljivki.

PHP vsebuje 3 vrste nizov, ki so naslednji:

  1. Indeksirano polje
  2. Asociativni niz
  3. Večdimenzionalni niz

1. Indeksirano polje

PHP indeks je opisan s celim številom, ki se začne z 0 (privzeta vrednost). Matrika PHP lahko shrani katero koli vrsto podatkov, kot so številke, znaki, nizi in predmeti. Vsem podatkom polja PHP je privzeto dodeljena indeksna številka.

Sintaksa za shranjevanje vrednosti

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

oz

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

Primer

 

Izhod

 Colours are: Red, White, Black, Yellow 

2. Asociativni niz

V PHP lahko uporabnik z uporabo simbola '=>' poveže katero koli specifično ime z vsakim elementom polja.

Sintaksa

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

oz

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

Primer

 <?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; 

Izhod

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

3. Večdimenzionalni niz

Večdimenzionalne nize v PHP imenujemo tudi 'niz nizov'. Uporabniku je omogočil shranjevanje matričnih podatkov v obliki tabele. Večdimenzionalno matriko PHP lahko izrazimo v obliki matrike, ki je označena z vrstico * stolpec.

java je prazna

Sintaksa

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

Primer

 <?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; 

Izhod

 1 Reema 95 2 john 45 3 rahul 20 

5. Python

Python uporablja modul z imenom 'Array' za upravljanje vseh funkcij matrik v Pythonu. Koristno je, ko želi uporabnik manipulirati samo z določenimi vrednostmi podatkov. Spodaj so navedene ključne besede, ki so pomembne za učenje koncepta matrike v Pythonu:

  • Element – ​​vsi podatki, shranjeni v matriki, so znani kot element.
  • Indeks – Kadar koli matrika shrani kakršne koli podatke, ima neko številčno lokacijo, znano kot indeks, ki je koristen za identifikacijo lokacije elementa.

Sintaksa

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

Primer

 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]) 

Izhod

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