[英]How to convert List of Double to List of String?
這對你們所有人來說可能太容易了,但我只是在一個項目中學習和實施 Java 並堅持這一點。
如何將Double
List
轉換為List
String
?
有很多方法可以做到這一點,但這里有兩個 styles 供您選擇:
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());
}
但更酷的方法是使用現代 collections API (我最喜歡的是Guava )並以更實用的風格執行此操作:
List<String> strings = Lists.transform(ds, new Function<Double, String>() {
@Override
public String apply(Double from) {
return from.toString();
}
});
您必須遍歷您的雙重列表並添加到新的字符串列表中。
List<String> stringList = new LinkedList<String>();
for(Double d : YOUR_DOUBLE_LIST){
stringList.add(d.toString());
}
return stringList;
List<Double> ds = new ArrayList<Double>();
// fill ds with Doubles
List<String> strings = ds.stream().map(op -> op.toString()).collect(Collectors.toList());
List<Double> doubleList = new ArrayList<Double>();
doubleList.add(1.1d);
doubleList.add(2.2d);
doubleList.add(3.3d);
List<String> listOfStrings = new ArrayList<String>();
for (Double d:doubleList)
listOfStrings.add(d.toString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.