有很多方法可以做到这一点,但这里有两种样式供您选择:
List<Double> ds = new ArrayList<Double>();
// fill ds with Doubles
List<String> strings = new ArrayList<String>();
for (Double d : ds) {
// Apply formatting to the string if necessary
strings.add(d.toString());
}
Run Code Online (Sandbox Code Playgroud)
但更酷的方法是使用现代集合API(我最喜欢的是Guava)并以更实用的方式执行此操作:
List<String> strings = Lists.transform(ds, new Function<Double, String>() {
@Override
public String apply(Double from) {
return from.toString();
}
});
Run Code Online (Sandbox Code Playgroud)
您必须迭代双列表并添加到新的字符串列表.
List<String> stringList = new LinkedList<String>();
for(Double d : YOUR_DOUBLE_LIST){
stringList.add(d.toString());
}
return stringList;
Run Code Online (Sandbox Code Playgroud)
小智 5
List<Double> ds = new ArrayList<>();
// fill ds with Doubles
List<String> strings = ds.stream().map(String::valueOf).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)