Formatting a string array in java

Tags: , , ,



I have a string array which has k elements. I want to print them out using System.out.format, but the issue is that I do not know k. So essentially, I want to use something like: System.out.format("%s %s ... k times", str1, str2, ... strk); (where k is a variable)

I was looking through the java documentation, but could not find a way to do this. Is there a simple way out?

Thanks!

Answer

Use a loop:

for (String s : array) {
    System.out.print(String.format("%s ", s));
}
System.out.println();


Source: stackoverflow