转换列表<整数>到列表<字符串> [英] Converting List<Integer> to List<String>
本文介绍了转换列表<整数>到列表<字符串>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个整数列表,List
,我想将所有整数对象转换为字符串,从而完成一个新的 List
代码>.
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.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));
}
这篇关于转换列表<整数>到列表<字符串>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文