So i wrote a simple code
int k=3;
if (k==3)
{ int a[i][j]=new int[10][10]; }
a[2][3]= 4;
System.out.println(a[2][3]);
I am getting an error in atom editor that cannot find symbol , I want my array to be initialized in if statement
So i wrote a simple code
int k=3;
if (k==3)
{ int a[i][j]=new int[10][10]; }
a[2][3]= 4;
System.out.println(a[2][3]);
I am getting an error in atom editor that cannot find symbol , I want my array to be initialized in if statement
int a[][]=new int[10][10]; // the bounds are taken care by the new declaration
I want my array to be initialized in if statement
Also if you put it under if
, that would be local to the if
. Hence what would work for you is:
if (k==3) {
int a[][]=new int[10][10];
a[2][3]= 4;
System.out.println(a[2][3]);
}
A better practice to declare an array that can be used not just locally would be somewhat like -
int a[][]=new int[10][10];
if (k==3) {
a[2][3]= 4;
}
System.out.println(a[2][3]);