简体   繁体   English

对AtomicInteger和打印二维数组有疑问

[英]doubts on AtomicInteger and on printing a bi-dimensional array

I have 2 doubts today. 我今天有2个疑问。 1) I am trying to print a bi-dimensional array (matrix Nx) and I am using this method: 1)我试图打印一个二维数组(矩阵Nx),并且我正在使用此方法:

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

the matrix has only int variables. 矩阵只有int变量。

This is the output, why? 这是输出,为什么?

[[I@15db9742, [I@6d06d69c, [I@7852e922, [I@4e25154f, [I@70dea4e, [I@5c647e05, ........etc

2) Using AtomicIntegers I have to set all matrix on 0. I used this code: 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);
        }
    }

but the Teacher's solution is: 但教师的解决方案是:

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);

Is there difference? 有区别吗? Is my code wrong? 我的代码错了吗?

Your code will throw a null pointer exception as it is trying to set value to a null object. 您的代码将尝试将值设置为空对象时,将引发空指针异常。 You have to initialise the variable first and then set value. 您必须先初始化变量,然后设置值。

关于您的第一个问题的使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM