2017-09-26 96 views
0

今天我有2个疑问。 1)我试图打印两维阵列(矩阵NX),我使用这种方法:关于AtomicInteger和打印二维数组的疑问

System.out.println(Arrays.toString(Matr)); 

矩阵只有诠释变量。

这是输出,为什么?

[[[email protected], [[email protected], [[email protected], [[email protected], [[email protected], [[email protected], ........etc 

2)使用的AtomicIntegers我必须设置为0所有矩阵我用这个代码:

AtomicInteger[][]Matr=new AtomicInteger[n][m]; 

    for(int i=0; i<n; i++) { 
     for(int j=0; j<m; j++) { 
      Matr[i][j].set(0); 
     } 
    } 

但老师的解决方案是:

AtomicInteger[][] A = new AtomicInteger[n][m]; 
    for (int i = 0; i < A.length; i++) 
     for (int j = 0; j < A[i].length; j++) 
      A[i][j] = new AtomicInteger(0); 

是否有区别?我的代码错了吗?

+0

你尝试运行的代码?你会看到不同之处。 – Eran

+0

好吧,看起来你有一个二维数组,这意味着两个字符串将只将第一个维度转换为一个字符串,因为它仍然包含数组,这不会产生太大的意义。您必须将第一维内的所有元素转换为字符串。另外,不要使用大写变量名称。 – ScriptKiddy

+0

太宽了。你已经问了两个不相关的问题作为一个问题。 –

回答

0

你的代码会抛出一个空指针异常,因为它试图将值设置为空对象。您必须首先初始化变量,然后设置值。

0

关于你的第一个问题使用