Is there any way I can print String array without using for loop?

JavaArraysString

Java Problem Overview


Is there any function in java like toString() to print a String array?

This is a silly question but I want to know if there is any other way than writing a for loop.

Thanks.

Java Solutions


Solution 1 - Java

String[] array = { "a", "b", "c" };
System.out.println(Arrays.toString(array));

Solution 2 - Java

With Apache Commons Lang,

System.out.println(StringUtils.join(anArray,","));

Solution 3 - Java

There is the Arrays.toString() method, which will convert an array to a string representation of its contents. Then you can pass that string to System.out.println or whatever you're using to print it.

Solution 4 - Java

If you need a bit more control over the string representation, Google Collections Joiner to the rescue!

String[] myArray = new String[] {"a", "b", "c"};
String joined = Joiner.on(" + ").join(myArray);
// =>  "a + b + c"

Solution 5 - Java

I think you are looking for

System.out.printf(String fmtString, Object ... args)

Where you specify the format of the output using some custom java markup (this is the only part you need to learn). The second parameter is the object, in your case, the array of strings.

More information: Using Java's Printf Method

Solution 6 - Java

With op4j,

String[] myArray = new String[] {"a", "b", "c"};

System.out.println(Op.on(myArray).toList().get());

Solution 7 - Java

String[] values= { ... }
System.out.println(Arrays.asList(values));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpriyankView Question on Stackoverflow
Solution 1 - JavaMikeView Answer on Stackoverflow
Solution 2 - JavaTheLQView Answer on Stackoverflow
Solution 3 - JavaDavid ZView Answer on Stackoverflow
Solution 4 - JavaSteven SchlanskerView Answer on Stackoverflow
Solution 5 - JavaaduView Answer on Stackoverflow
Solution 6 - JavadomgomView Answer on Stackoverflow
Solution 7 - JavaSteve B.View Answer on Stackoverflow