+ 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); } }
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);
+ 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
+ 4
@ Lambor yep here you go..
https://code.sololearn.com/c9pLwXYRRzcb/?ref=app
+ 3
I guess, because you are trying to access an array field that does not exist. Remember, index starts at 0.
+ 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
+ 2
@D_Stark can you create a code for that? for example
+ 2
you are really amazing bro. now I understood clearly
+ 1
there is no [1][1]
didn't u read postes?!
0
@Torsten good point
@lambor read the "multidimensional arrays". lesson....
0
actually I need to insert 5 on [1][1]
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