gpt4 book ai didi

java - 如何从ArrayList中获取所需的格式

转载 作者:行者123 更新时间:2023-12-02 21:24:54 25 4
gpt4 key购买 nike

import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class Test {
final static Logger logger = Logger.getLogger(Test.class);
private static DecimalFormat decimal_inpoints = new DecimalFormat("0.00");

public static void main(String args[]) throws UnknownHostException,
ParseException {

ArrayList<Integer> array_list = new ArrayList<Integer>();

array_list.add(1);
array_list.add(0);

String joinedString = array_list.toString();

System.out.println(joinedString);

}

}

如何将输出设为 1,0

当我使用 array_list.toString() 时;其给出的输入为 [1,0](已添加数组)

你能告诉我如何得到 1,0 而不是 [1,0]

最佳答案

使用Apache Commons Lang :

String join = StringUtils.join(joinList, ",");

使用 Java 8

String joined = String.join(",", list);

使用 Google Guava (Joiner)

Joiner joiner = Joiner.on(",");
String join = joiner.join(joinList);

使用 StringBuilder

StringBuilder builder = new StringBuilder(array_list.size());
boolean isFirst = true;
for (int i : array_list){
if (isFirst) {
builder.append(i);
isFirst = false;
} else {
builder.append(", " + i);
}
}
System.out.println(builder.toString());

关于java - 如何从ArrayList中获取所需的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35797994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com