問(wèn)題描述
我有一個(gè)整數(shù)列表,List
,我想將所有整數(shù)對(duì)象轉(zhuǎn)換為字符串,從而完成一個(gè)新的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>
.
當(dāng)然,我可以創(chuàng)建一個(gè)新的 List<String>
并循環(huán)遍歷每個(gè)整數(shù)調(diào)用 String.valueOf()
的列表,但我想知道是否有一種更好的(閱讀:更自動(dòng)化)的方法?
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?
推薦答案
據(jù)我所知,迭代和實(shí)例化是唯一的方法.類似的東西(對(duì)于其他人可能的幫助,因?yàn)槲蚁嘈拍阒廊绾巫龅竭@一點(diǎn)):
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));
}
這篇關(guān)于轉(zhuǎn)換列表<整數(shù)>列出<字符串>的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!