假设我有一个int,float,string等数组。是否有任何实用程序API(例如Commons,Guava)会给我一个逗号分隔的字符串?

像这样

int[] a = {1,2,3,4,5}.
String s = magicAPI.getCSV(a); // s == "1,2,3,4,5";

最佳答案

我过去使用过OpenCSV

StringWriter stringWriter = new StringWriter();
int[] a = {1,2,3,4,5};
String[] b = new String[a.length];
for ( int i = 0; i < a.length; i++) {
    b[i] = a[i];
}
CSVWriter csvWriter = new CSVWriter(stringWriter, ",");
csvWriter.writeNext(b);

但是,对于这样一个简单的示例,您可能只想使用StringBuilderfor循环

09-05 09:20