Is there a way to print an array of strings without using a loop?

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

This is a stupid question, but I want to know if there is another way than writing a for loop.

Thank.

+50
java string arrays
Aug 14 '10 at 0:36
source share
7 answers
String[] array = { "a", "b", "c" }; System.out.println(Arrays.toString(array)); 
+84
Aug 14 2018-10-10T00: 00Z
source share

With Apache Commons Lang ,

 System.out.println(StringUtils.join(anArray,",")); 
+12
Aug 14 2018-10-10T00:
source share

There is an Arrays.toString() method that converts an array to a string representation of its contents. You can then pass this line to System.out.println or whatever you use to print it. A.

+4
Aug 14 '10 at 0:40
source share

If you need a little more control over the string representation, Google Collections Joiner is there to help!

 String[] myArray = new String[] {"a", "b", "c"}; String joined = Joiner.on(" + ").join(myArray); // => "a + b + c" 
+2
Aug 14 '10 at 1:29
source share

I think you are looking

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

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

Additional Information: Using the Java Printf Method

+1
Aug 14 2018-10-10 at
source share

With op4j ,

 String[] myArray = new String[] {"a", "b", "c"}; System.out.println(Op.on(myArray).toList().get()); 
+1
Aug 14 '10 at 9:32
source share
 String[] values= { ... } System.out.println(Arrays.asList(values)); 
0
Aug. 14 2018-10-10T00:
source share



All Articles