+ 1
Why 'null' and NullPointerException is occurring?
1) int[] a =new int[3]; Sop(a); //[|@3e25a5 Sop(a[0]); //0 2) int[][] a =new int[2][3]; Sop(a); //[|@3e25a5 Sop(a); //[|@19821f Sop(a[0][0]); //0 3) int[][] a =new int[2][]; Sop(a); //[|@3e25a5 Sop(a[0]); //null Sop(a[0][0]); //R.E:NullPointerException
13 odpowiedzi
+ 4
import java.util.Arrays;
public class Program {
public static void main(String[] args) {
// int[][] a = new int[2][3];
// is shortcut to
int[] array1 = {0,0,0};
int[] array2 = {0,0,0};
int[][] a = {array1, array2};
System.out.println( Arrays.deepToString(a));
// int[][] aa = new int[2][];
// is shortcut to
int[][] aa = {null, null};
System.out.println( Arrays.deepToString(aa));
}
}
+ 1
3rd example creates
a[null, null]
null[0] throws Exception
+ 1
Yes please any one explain
0
Please explain more
0
Tysm
0
In first
There's 2 row and at Every row 3 column
But in second there's 2 row but no column so
There's like this {{null,null},{null,null}};
?
0
aa is internally first array of Objects int[],
if there are not elements, default value is null
not empty objects
Object[] b = new Object[2];
System.out.println( Arrays.toString(b));
// [null, null]
// compare to this
Object[] d = new Object[2];
Object c1 = new int[3];
Object c2 = new int[3];
d[0] = c1;
d[1] = c2;
System.out.println( Arrays.deepToString(d));
// [[0, 0, 0], [0, 0, 0]]
0
> ".. in second there's 2 row but no column"
by [2][] length of "column" is not specified,
but you expect length 2
{{null,null},{null,null}}; // wrong
row = {{column1},{column2}};
0
Why not 0
earlier there's 0
here's null
how can I know where's 0 or where's null...
0
for int is default 0
for Object is default null
array int[] is internally Object
0
Then what's the difference in example num 2) and no 3) both have new and int keyword then num 2) keyword have 0 and num 3) have null....
0
you need understand it is not simple one 2D data type, like matrix(x,y) with same elements.
this implementation is complex of 1D arrays where one has different type of elements
0
Ok, thank you