开发者

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

This question already has answers here: What's the simplest way to print a Java array? (37 answers) Closed 7 years ago.

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.


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


With Apache Commons Lang,

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


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.


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"


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


With op4j,

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

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜