問題描述
我有一個整數列表,List
,我想將所有整數對象轉換為字符串,從而完成一個新的List<String>
代碼>.
I have a list of integers, List<Integer>
and I'd like to convert all the integer objects into Strings, thus finishing up with a new List<String>
.
當然,我可以創建一個新的 List<String>
并循環遍歷每個整數調用 String.valueOf()
的列表,但我想知道是否有一種更好的(閱讀:更自動化)的方法?
Naturally, I could create a new List<String>
and loop through the list calling String.valueOf()
for each integer, but I was wondering if there was a better (read: more automatic) way of doing it?
推薦答案
據我所知,迭代和實例化是唯一的方法.類似的東西(對于其他人可能的幫助,因為我相信你知道如何做到這一點):
As far as I know, iterate and instantiate is the only way to do this. Something like (for others potential help, since I'm sure you know how to do this):
List<Integer> oldList = ...
/* Specify the size of the list up front to prevent resizing. */
List<String> newList = new ArrayList<>(oldList.size());
for (Integer myInt : oldList) {
newList.add(String.valueOf(myInt));
}
這篇關于轉換列表<整數>列出<字符串>的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!