Java – the best way to convert a numeric array to a CSV string?

If I have a string [] (assuming no comma), I can simply generate a CSV line For example,

String[] header = {"header0","header1","header2"};
String joined = String.join(",",header);

What is a good way to use int [] vals01 = {0,1,2} for equivalence? (I consider using arrays.tostring and cutting off both ends is ugly.)

Solution

This can be done through the streams API in a single line:

Arrays.stream(new int[] {0,2}).mapToObj(String::valueOf).collect(joining(","));

(suppose you import static Java. Util. Stream. Collectors. Joining)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>