Can anyone explain why this code not executing | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can anyone explain why this code not executing

public class Program { public static void main(String[] args) { int [][] myarr ={{1,2,3},{4},{6,7,8}}; myarr[1][1] = 5; int x = myarr[1][1]; System.out.println(x); } }

9th Mar 2018, 5:49 AM
Marana Coder
Marana Coder - avatar
11 Answers
+ 5
@Torsten is correct if your trying to change the 4 to 5 in the array then you need myarr[1][0] = 5; int x = myarr[1][0]; System.out.println(x);
9th Mar 2018, 6:21 AM
D_Stark
D_Stark - avatar
+ 5
@lambor exactly you cant increase the size of the array once its been declared you would have to create a new array at a larger size and copy all existing values over
9th Mar 2018, 8:01 AM
D_Stark
D_Stark - avatar
9th Mar 2018, 8:49 AM
D_Stark
D_Stark - avatar
+ 3
I guess, because you are trying to access an array field that does not exist. Remember, index starts at 0.
9th Mar 2018, 5:54 AM
Torsten
+ 2
okkk. now I got that. Once we done initializing an array it's fixed. in the middle of the program we can't insert the value to array. tnq
9th Mar 2018, 7:31 AM
Marana Coder
Marana Coder - avatar
+ 2
@D_Stark can you create a code for that? for example
9th Mar 2018, 8:22 AM
Marana Coder
Marana Coder - avatar
+ 2
you are really amazing bro. now I understood clearly
9th Mar 2018, 8:53 AM
Marana Coder
Marana Coder - avatar
+ 1
there is no [1][1] didn't u read postes?!
9th Mar 2018, 7:26 AM
Farshaad Heydari
Farshaad Heydari - avatar
0
@Torsten good point @lambor read the "multidimensional arrays". lesson....
9th Mar 2018, 6:05 AM
Farshaad Heydari
Farshaad Heydari - avatar
0
actually I need to insert 5 on [1][1]
9th Mar 2018, 7:13 AM
Marana Coder
Marana Coder - avatar
0
arrays are fixed size..... meaning that once u create the array int [][] myarr ={{1,2,3},{4},{6,7,8}}; you only can change the value of existing index's and u can't add a new index(a new slot or place holder) in the array
9th Mar 2018, 7:29 AM
Farshaad Heydari
Farshaad Heydari - avatar