0

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?

Mohit Tyagi
  • 2,788
  • 4
  • 17
  • 29
DarkPassenger
  • 67
  • 1
  • 8
  • Did you try to run your code? You'll see the difference. – Eran Sep 26 '17 at 10:52
  • Well, looks like your have a two dimensional array, meaning that two string will turn only the first dimension into a string, since it still contains arrays, that won't make too much sense. you gotta turn all the elements inside of the first dimension into strings instead. Also, dont use uppercase variable names. – Marcel Sep 26 '17 at 10:54
  • Too broad. You have asked two unrelated questions as a single Question. – Stephen C Sep 26 '17 at 11:22

2 Answers2

0

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.

L.H
  • 81
  • 6
0

Regarding to your first question use

System.out.println(Arrays.deepToString(Matr));
Eritrean
  • 15,851
  • 3
  • 22
  • 28