0

I am trying to print a multidimensional array filled with *s. here is the code.

String[][] star = new String[10][10];

for(String[] row: star)
{
    Arrays.fill(row, "*");      
}

System.out.println(Arrays.toString(star)); 
// prints something like 'Ljava.lang.String;@592fa617'

But I can't seem to print out what i want...what am I doing wrong?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
user3773503
  • 161
  • 2
  • 5
  • 12

4 Answers4

4

try this

System.out.println(Arrays.deepToString(star));
rock321987
  • 10,942
  • 1
  • 30
  • 43
0

You can use this:

for (int i=0; i<star.length;i++) {
        for (int j=0; j<star[i].length;j++) {
            System.out.println(star[i][j]);
        }
}

Arrays by default don't print content.

Nikhil Talreja
  • 2,754
  • 1
  • 14
  • 20
  • Alright thanks. so does that mean I can't print the whole array at a time? Do i always have to use a loop to print each element individually? – user3773503 Jun 25 '14 at 03:58
  • You may use `Arrays.deepToString` as suggested by other users. I am not sure how this is internally implemented though. – Nikhil Talreja Jun 25 '14 at 04:03
0

You should use either the Arrays.deepToString method or foreach loops.

Bartosz Mikulski
  • 525
  • 3
  • 17
0

Well I can show you two ways to do it :

First Way :

        for(int i=0;i<10;i++)
        {
            System.out.println();
            for(int j=0;j<10;j++)
                System.out.print(star[i][j]+"\t");
        }

Second way :

java.util.Arrays.deepToString() which Returns a string representation of the "deep contents" of the specified array.

System.out.println(Arrays.deepToString(star));
SparkOn
  • 8,806
  • 4
  • 29
  • 34