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?

0

4 Answers 4

4

try this

System.out.println(Arrays.deepToString(star));
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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?
You may use Arrays.deepToString as suggested by other users. I am not sure how this is internally implemented though.
0

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.