elements inside a multidimensional array are arrays themselves. Multidimensional Arrays can be thought of as an array inside the array i.e. sum of the items of all types that a factory produces.An array is a homogeneous data structure that stores data of the same type in contiguous memory locations. We have to calculate the total products of each factory i.e. Suppose there are 2 factories and each of these factories produces items of 4 different types, like some items of type 1, some items of type 2 and so on. Let’s see another example of a 2-dimensional array. Similarly, in the second and third iterations of the outer for loop, the same process is repeated for student 2 and student 3 respectively.Īfter assigning the values to the elements of the array, the values of the elements of the array are printed in the same way using a for loop inside another for loop. In its body, “ Subject : 3” gets printed and the value entered by the user is assigned to marks. In the third iteration of the inner for loop, the value of i is 0 and that of j is 2, and thus marks is marks. In its body, “ Subject : 2” gets printed and the value entered by the user is assigned to marks. In the second iteration of the inner for loop, the value of i is 0 and that of j is 1, and thus marks is marks0]. In its body, “ Subject : 1” gets printed and the statement marks = s.nextDouble() assigns the value entered by the user to marks. In the first iteration of the inner for loop, the value of i is 0 and that of j is 0, and thus marks is marks. In its body, “ Enter marks of student : 1” gets printed and the inner for loop gets executed. In the first iteration of the outer for loop, the value of i is 0. The elements of the array will contain the marks of the 3 students in the 2 subjects as shown below.įirst, the array is initialized with the values entered by the user using a for loop inside another for loop. In this example, an array named marks having 3 rows and 2 columns is defined by writing double marks = new double. Let's see its example.Įnter marks of student : 1 Subject : 1 10 Subject : 2 20 Enter marks of student : 2 Subject : 1 30 Subject : 2 10 Enter marks of student : 3 Subject : 1 20 Subject : 2 40 Student1 Subject1:10.0 Subject2:20.0 Student2 Subject1:30.0 Subject2:10.0 Student3 Subject1:20.0 Subject2:40.0 There is no official name of this loop, but the term for-each is used most often. Similarly, we can iterate over an array using while and do.while loops as well. In this way, we used the first for loop for taking user input for the elements of the array and the second for loop to print the elements of the array. In each iteration, an element of the array is printed. In the second for loop, there are five iterations from i = 0 till i = 4. In this way, values are assigned to all the five elements of the array in the five iterations. Again an integer entered by the user is read and assigned to myarray (second element of the array). In the second iteration, the value of i is 1. The statement myarray = s.nextInt() reads the integer entered by the user and assigns it to myarray (first element of the array). In the first iteration, the value of i is 0. In the first for loop, there are five iterations from i = 0 till i = 4. In the second loop, we are printing the values of the elements of the array. In the first loop, we are assigning the values entered by the user to the elements of the array. Thus, the index of the elements in the array is from 0 to 5.Įnter 5 integers of array 11 34 20 50 60 Elements of the array are: 11 34 20 50 60 In the above example of array arr, the index of 8 is 0, 10 is 1, 2 is 2, 7 is 3, 4 is 4 and 56 is 5. Therefore, the index of the first element is 0, the index of the second element is 1, and so on. ![]() elementĮvery element in an array has a unique index. The pictorial view of the array arr is shown below. ![]() We can also say that 8, 10, 2, 7, 4 and 56 are the elements of the array. Class Test īoth the above statements declare an array named arr and store the integers 8, 10, 2, 7, 4 and 56 in it.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |