如何在Java中将逗号分隔的字符串转换为ArrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18104278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a comma separated String to ArrayList in Java
提问by
I have a comma separated String which i need to convert to ArrayList . I tried this way
我有一个逗号分隔的 String ,我需要将其转换为 ArrayList 。我试过这种方式
import java.util.ArrayList;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
String CommaSeparated = "item1 , item2 , item3";
ArrayList<String> items = (ArrayList)Arrays.asList(CommaSeparated.split("\s*,\s*"));
for(String str : items)
{
System.out.println(str);
}
}
}
Its giving me the Runtime Error as shown
它给了我运行时错误,如图所示
Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
at com.tradeking.at.process.streamer.Test.main(Test.java:14)
as i was trying to convert an List to arrayList by force .
因为我试图强制将 List 转换为 arrayList 。
采纳答案by Rohit Jain
The ArrayList
returned by Arrays.asList
is not java.util.ArrayList
. It's java.util.Arrays.ArrayList
. So you can't cast it to java.util.ArrayList
.
该ArrayList
由归国Arrays.asList
不是java.util.ArrayList
。它是java.util.Arrays.ArrayList
。所以你不能把它投射到java.util.ArrayList
.
You need to pass the list to the constructor of java.util.ArrayList
class:
您需要将列表传递给java.util.ArrayList
类的构造函数:
List<String> items = new ArrayList<String>(Arrays.asList(CommaSeparated.split("\s*,\s*")));
or, you can simply assign the result:
或者,您可以简单地分配结果:
List<String> items = Arrays.asList(CommaSeparated.split("\s*,\s*"));
but mind you, Arrays.asList
returns a fixed size list. You cannot add or remove anything into it. If you want to add or remove something, you should use the 1st version.
但请注意,Arrays.asList
返回一个固定大小的列表。您不能在其中添加或删除任何内容。如果要添加或删除某些内容,则应使用第一个版本。
P.S: You should use List
as reference type instead of ArrayList
.
PS:您应该使用List
作为引用类型而不是ArrayList
.
回答by Kayaman
You can't just cast objects around like they're candy. Arrays.asList()
doesn't return an ArrayList
, so you can't cast it (it returns an unmodifiable List).
你不能像扔糖果一样随意扔东西。Arrays.asList()
不返回ArrayList
,所以你不能投射它(它返回一个不可修改的列表)。
However you can do new ArrayList(Arrays.asList(...));
但是你可以做 new ArrayList(Arrays.asList(...));
回答by Chris Thompson
Does it absolutely need to be an ArrayList
? Typically you want to use the most generic form. If you're ok using a List
just try:
它绝对需要是一个ArrayList
吗?通常,您希望使用最通用的形式。如果您可以使用List
,请尝试:
List<String> items = Arrays.asList(...);
You can still iterate over it the same way you currently are.
您仍然可以像当前一样迭代它。
回答by Kwak Young Ho
String CommaSeparated = "item1 , item2 , item3";
ArrayList<String> items = new ArrayList(Arrays.asList(CommaSeparated.split("\s*,\s*")));
for(String str : items)
{
System.out.println(str);
}
回答by nilesh patel
Please try to use bellow code.
请尝试使用以下代码。
String[] temp;
字符串[] 温度;
/* delimiter */
String delimiter = ",";
/*
* given string will be split by the argument delimiter provided.
*/
temp = parameter.split(delimiter);
ArrayList<String> list = new ArrayList<String>();
for (int l = 0; l < temp.length; l++)
{
list.add(temp[l]);
}