logo

Ako uložiť hodnotu do poľa

Pole je skupina prvkov podobného typu, ktoré majú súvislé miesto v pamäti. Pole je kolekcia premenných podobného typu, ktoré sú označené spoločným názvom.

Tento tutoriál stručne odhalí, ako uložiť hodnotu do poľa v bežne používaných jazykoch.

1. Jazyk C

Všetky polia sú súvislým blokom pamäťových miest. V predvolenom nastavení je na najnižšej pozícii poľa uložený prvý prvok a na najvyššej pozícii sú uložené posledné údaje. V jazyku C je pole deklarované zadaním typu prvku a celkovej dĺžky poľa potrebnej na uloženie údajov.

Syntax na deklarovanie poľa

 type arrayName [ arrSize ]; 

Syntax pre inicializáciu na ukladanie hodnôt poľa

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

Príklad

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

Multidimenzionálne pole v C

V jazyku C sa k prvkom 2D (dvojrozmerného) poľa pristupuje pomocou dolných indexov, t. j. indexového čísla riadka a indexového čísla stĺpca poľa.

Syntax na deklarovanie poľa

 int val = arr[x][y]; 

Syntax pre inicializáciu dvojrozmerných polí

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

V jazyku C++ musí používateľ špecifikovať typ prvku a celkovú dĺžku poľa.

Syntax deklarovať pole

 type arrName [ arrSize ]; 

Syntax na inicializáciu poľa

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

Príklad

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

Multidimenzionálne pole

Jazyk C++ umožňuje aj viacrozmerné polia.

Syntax pre inicializáciu 2D poľa

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

Príklad

 #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 jazyku Java fungujú polia inak ako v jazyku C alebo C++.

Jednorozmerné polia:

Na deklarovanie poľa musí mať používateľ dve primárne komponenty: typ a názov poľa.

„Typ“ sa vzťahuje na elementárny typ konkrétneho poľa. Určuje typ údajov všetkých prvkov, ktoré sú zahrnuté v poli. Zahŕňa pole primitívnych dátových typov, na rozdiel od celých čísel, char, float, double atď., alebo môže zahŕňať aj užívateľom definované dátové typy (objekty triedy). Preto typ prvku pre pole uzatvára, aký druh údajov bude pole obsahovať.

sql ddl príkazy

Syntax

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

Uložte hodnoty do jednorozmerného poľa

Priradenie hodnôt k prvku v poli je podobné ako priradenie hodnôt skalárnym premenným.

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

POZNÁMKA: Ak prvku poľa nie je priradená žiadna hodnota, štandardne má hodnotu Null (prázdnu).

Príklad

 //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++)>

Pole objektov

Pole objektov je konštruované rovnakým spôsobom ako pole dátových prvkov primitívneho typu.

Príklad

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

Viacrozmerné polia

Viacrozmerné polia sa nazývajú „polia polí“, pretože môžu obsahovať každý prvok poľa s odkazom na iné pole. Tieto sú tiež známe ako Jagged Arrays. Viacrozmerné pole sa vytvorí pridaním množiny hranatých zátvoriek ([]) pre každý rozmer.

Syntax

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

Príklad na uloženie hodnôt do viacrozmerného poľa

 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; 

Príklad viacrozmerného poľa

 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 pole je usporiadaná mapa (uchováva prvky na báze kľúč-hodnota). Používa sa na uchovávanie viacerých hodnôt podobného typu údajov v jednej premennej.

cesta nastavená v jazyku Java

PHP obsahuje 3 druhy polí, ktoré sú nasledovné:

  1. Indexované pole
  2. Asociatívne pole
  3. Multidimenzionálne pole

1. Indexované pole

PHP index je opísaný celým číslom, ktoré začína 0 (predvolená hodnota). Pole PHP môže uchovávať akýkoľvek typ údajov, ako sú čísla, znaky, reťazce a objekty. Všetky údaje poľa PHP majú štandardne pridelené indexové číslo.

Syntax na ukladanie hodnôt

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

Alebo

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

Príklad

 

Výkon

 Colours are: Red, White, Black, Yellow 

2. Asociatívne pole

V PHP môže užívateľ priradiť akýkoľvek špecifický názov ku každému prvku poľa pomocou symbolu '=>'.

Syntax

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

Alebo

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

Príklad

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

Výkon

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

3. Multidimenzionálne pole

Viacrozmerné polia v PHP sa tiež nazývajú „pole polí“. Používateľovi to umožnilo ukladať údaje poľa v tabuľkovom formáte. PHP viacrozmerné pole môže byť vyjadrené vo forme matice, ktorá je označená riadkom * stĺpcom.

Syntax

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

Príklad

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

Výkon

 1 Reema 95 2 john 45 3 rahul 20 

5. Python

Python používa modul s názvom 'Array' na spracovanie všetkých funkcií polí v Pythone. Je to užitočné, keď chce používateľ manipulovať iba s konkrétnymi hodnotami údajov. Nižšie sú uvedené kľúčové slová, ktoré sú dôležité na naučenie sa konceptu poľa v Pythone:

  • Element – ​​Všetky dáta uložené v poli sú známe ako element.
  • Index – Kedykoľvek pole ukladá akékoľvek údaje, má nejaké číselné umiestnenie známe ako index, ktoré je výhodné na identifikáciu umiestnenia prvku.

Syntax

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

Príklad

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

Výkon

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