+ 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

5th Jul 2017, 12:49 PM
Sheevika
9 odpowiedzi
+ 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 .
5th Jul 2017, 1:04 PM
RZK 022
RZK 022 - avatar
+ 2
myarr[0][2]=42 here in this line 42 is assigned in the array.. and replaces 3..
5th Jul 2017, 1:24 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 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 ?
5th Jul 2017, 1:11 PM
Jojo
0
If myArr[0][2]=42 is part of your code, it's an like i=3; It's an affectation.
5th Jul 2017, 12:52 PM
Jojo
0
Still not getting it, sorry. What is the purpose of that line of code? It was in the tutorial without any explanation
5th Jul 2017, 12:59 PM
Sheevika
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
5th Jul 2017, 1:08 PM
Sheevika
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).
5th Jul 2017, 1:23 PM
Sheevika
0
Nothing will output
5th Jul 2017, 1:24 PM
Jojo
0
Is that alright ? Ask if it's not !
5th Jul 2017, 1:25 PM
Jojo