简体   繁体   中英

doubts on AtomicInteger and on printing a bi-dimensional array

I have 2 doubts today. 1) I am trying to print a bi-dimensional array (matrix Nx) and I am using this method:

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

the matrix has only int variables.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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