+ 2
int[ ][ ] myArr = { {1, 2, 3}, {4}, {5, 6, 7} }; myArr[0][2] = 42; int x = myArr[1][0]; // 4
I don't get how myArr[0][2]=42 Shouldn't myArr[0][2]=3? I must be missing something super basic, but can't figure it out
9 Réponses
+ 3
here we have a 3 dimensional array .
1st element - {1,2,3} // counted as 0
here sub elements are -
1- // counted 0
2- // counted 1
3- // counted 2;
so here
myArr[0][0] is 1
myArr[0][1] is 2
myArr[0][2] is 3
for second element we have {4} // taken as 1
so here only one element is there so there is only one possible value myArr[1][0] = 4
for third we have {5,6,7} // taken as 2
here there are again 3 possibilities
myArr[2][0] is 5
myArr[2][1] is 6
myArr[3][2] is 7
// hope that helps .
+ 2
myarr[0][2]=42
here in this line 42 is assigned in the array.. and replaces 3..
+ 1
myArr[0][2]=42 changes the value of myArr[0][2], wich was 3, to 42.
In Java there is '=' and '=='.
'=' MODIFY without VERIFY the value of the variable on the LEFT of it.
'==' VERIFY without MODIFY both values (left and right)
Do you understand ?
0
If myArr[0][2]=42 is part of your code,
it's an like i=3; It's an affectation.
0
Still not getting it, sorry. What is the purpose of that line of code?
It was in the tutorial without any explanation
0
thanks :)
But, my problem is, I don't understand how myArr[0][2] = 42.
According to what you just showed me, and what i understood from the lesson, given this array, myArr[0][2] should be equal to 3. But for some reason it says 42 on the tutorial
0
Yes, thanks Jojo.
One more question though: so basically this is a piece of code that doesn't do anything here? (except say that myArr[0][2] is equal to 42).
0
Nothing will output
0
Is that alright ? Ask if it's not !