Converting Listlt;Integergt; to Listlt;Stringgt;(转换列表lt;整数gt;列出lt;字符串gt;)
问题描述
我有一个整数列表,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));
}
这篇关于转换列表<整数>列出<字符串>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:转换列表<整数>列出<字符串>


- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01