+ 2

I don't really understand those multidimensional arrays

please, can someone explain me why the two of arrays [0][2] together are equal to 42 and how we calculate them later in System.out.printIn(x); as soon as int x=myArr[1][0]; public class Program { public static void main(String[] args) { int[ ][ ] myArr = { {1, 2, 3}, {4}, {5, 6, 7} }; myArr[0][2] = 42; int x = myArr[1][0]; System.out.println(x); } } https://code.sololearn.com/cKs8tGxoDBUj/?ref=app

16th Apr 2017, 10:22 PM
Rareş Daniel
Rareş Daniel - avatar
8 Respuestas
+ 9
I agree with you, multidimensional arrays can get confusing, especially when it's more than 2D (two dimensional) for me. There is a simple way to understand it better though and that's by picturing it. A two dimensional array can be pictured as a table, a table that has rows and columns. Let's create a 3x3 table quickly to demonstrate this. Example (Java): 0 1 2 ___________ 0 l_a_l_b_l_c_l 1 l_d_l_e_l_ f_l 2 l_g_l_h_l_ i_l -The table above is a 3x3 table, meaning it has 3 rows and 3 columns -Each cell of the table is filled with a different letter -If I want to identify where a letter is, I would need two coordinates right? A coordinate for rows and a coordinate for columns -So let's say I want to get letter "a" then it would be in row(0) and in column(0) right? -Let's try to get letter "f", it would be in row(1) in column(2) right? -So this is really the simplest way to understand multidimensional arrays by picturing it. -Let's now put this in a coding perspective, let's say I want to change the letter "f" to letter "z" then it would look something like this: table[1][2] = "z"; -Does it look simpler now with my explanation or are you still confused? Let me know.
16th Apr 2017, 10:48 PM
Ghauth Christians
Ghauth Christians - avatar
16th Apr 2017, 10:29 PM
Tashi N
Tashi N - avatar
+ 6
In your code here you have defined a matrix called myArr with 3 rows and 3 columns which you can visualize as: 1 2 3 4 5 6 7 The statement myArr[0][2] = 42 assigns value .42 to 0th row and 2nd col so ur matrix now becomes 1 2 42 4 5 6 7
16th Apr 2017, 10:29 PM
Shraddha
Shraddha - avatar
+ 6
Similarily myArr[1][0] means element at 1st row 0th col that is 4 in your matrix. This yor are assigning to x and printing. So the output of your code is 4
16th Apr 2017, 10:31 PM
Shraddha
Shraddha - avatar
+ 3
This is Solution : Don't forget to Upvote ................................................... using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int[,] num = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; //your code goes here for(int i=0; i<3;i++) { for(int j=0;j<3;j++) { Console.WriteLine(num[i,j]); } } } } }
26th Aug 2021, 7:11 AM
Khan Baz Khan Jadoon
Khan Baz Khan Jadoon - avatar
+ 2
Gavin your explanation is so easy to understand, at first i didn't knew that it refer to a matrix cause i knew that the matrix starts with [1][1] not [0][0]. Thanks a lot guys, i really appreciate.
16th Apr 2017, 10:55 PM
Rareş Daniel
Rareş Daniel - avatar
+ 1
i already do matrix at math class but those doesn't seem to look like that ones. it is a little complicated. But thank you for helping me. :)
16th Apr 2017, 10:34 PM
Rareş Daniel
Rareş Daniel - avatar
+ 1
Thanks Tashi N, You are the best. :)
16th Apr 2017, 10:38 PM
Rareş Daniel
Rareş Daniel - avatar