Home > Software engineering >  How do I fill my 2D array with a string ? Java
How do I fill my 2D array with a string ? Java

Time:09-15

I am using Arrays.fill() for integers but i dont know how to fill with string. I want it to look like this on the terminal screen:

A A A

A A A

A A A

CodePudding user response:

One immediate solution I can think of is using Java Streams.

String[][] table = new String[3][3];
Arrays.stream(table).forEach(strings -> Arrays.fill(strings,"A"));

It will give you output

[A, A, A]
[A, A, A]
[A, A, A]

To Print You can use

Arrays.stream(table).forEach(strings -> System.out.println(Arrays.toString(strings)));
  • Related