0
int[ ][ ] myArr = { {1, 2, 3}, {4}, {5, 6, 7} }; myArr[0][2] = 42; int x = myArr[1][0]; sop(x);
I know answer but I need explanation..I don't know anything about multidimensional array
3 ответов
+ 3
You r welcome! Happy coding with Java!
+ 2
{1,2,3},{4},{5,6,7} are members of first curly parantheses " { } ",I mean first dimension of the array.So,you can think them as a,b,c of first dimension,just like that:
MyArr[0]=a={1,2,3}
MyArr[1]=b={4}
MyArr[2]=c={5,6,7}
First two brackets "[ ]" represents the first dimension
Second two brackets "[ ]" represents the sesond dimension.
For example,we already knew the first element of first dimension,which is "a",namely {1,2,3} in this case.Then think this element lonely from the first dimension,because you are now inside of the second dimension.
And,you know that first two brackets show first dimesion and second two brackets show second dimension,briefly:
MyArr[0][..]equls to first element of first dimension,which was {1,2,3},and now you are ready to go inside of this dimension.(and because "{ }" are used,in this dimension,you are working with arrays again).
Just think {1,2,3} as another array inside of an array.
In MyArr[0][2] ,0 gives the location of first element in first dimension and 2 gives the third element of the second dimension.
(Third,because,in arrays counting strarts with 0 and goes 1,2 and etc.You know this from one dimensional arrays)
What is first element of first dimension:{1,2,3},you are right!
And what is the third element of {1,2,3}-second dimension-?
MyArr[0][0]=1
MyArr[0][1]=2
MyArr[0][2]=3
And you can change the values of each elements by assigning
After MyArr[0][2]= 42
You will get {1,2,42}
+ 1
fatma thank u so much..