将字符串数组转换为 java.util.List [英] Converting String array to java.util.List

查看:33
本文介绍了将字符串数组转换为 java.util.List的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 String 数组转换为 java.util.List?

How do I convert a String array to a java.util.List?

推荐答案

List<String> strings = Arrays.asList(new String[]{"one", "two", "three"});

这是数组的列表视图,列表部分不可修改,不能添加或删除元素.但是时间复杂度是O(1).

This is a list view of the array, the list is partly unmodifiable, you can't add or delete elements. But the time complexity is O(1).

如果你想要一个可修改的列表:

If you want a modifiable a List:

List<String> strings = 
     new ArrayList<String>(Arrays.asList(new String[]{"one", "two", "three"}));

这会将源数组中的所有元素复制到一个新列表中(复杂度:O(n))

This will copy all elements from the source array into a new list (complexity: O(n))

这篇关于将字符串数组转换为 java.util.List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆