将 ArrayList<String> 转换为 java.util.List
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16178327/
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
Convert ArrayList<String> to java.util.List
提问by john
I'm having trouble wrapping my head around a concept. I'm trying to convert a ArrayList<String>
to a List
(or List<String>
), but I keep running into a cast exception. Yes I saw the code to convert a string array to a list, but I feel this a different scenario. my relevant code is here, please note the declarations are because I'm using GWT and thus have to declare some things as static and final...
我无法围绕一个概念进行思考。我正在尝试将 a 转换ArrayList<String>
为 a List
(或List<String>
),但我一直遇到强制转换异常。是的,我看到了将字符串数组转换为列表的代码,但我觉得这是一个不同的场景。我的相关代码在这里,请注意声明是因为我使用的是 GWT,因此必须将某些内容声明为静态和最终...
private static List<String> values;
Ioma.dataservice.getPendingOrders(new AsyncCallback<ArrayList<Order>>(){
@Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ArrayList<Order> result) {
ArrayList<String> orderNumbers = new ArrayList<String>();
//problem here, cannot cast arraylist<string> to list string for some reason? idk. SO it maybe?
for(int i=0;i<result.size();i++){
System.out.println("ordernumber: " + result.get(i).getOrderNumber());
System.out.println("arraysize: " + result.size());
orderNumbers.add(Integer.toString(result.get(i).getOrderNumber()));
}
for (int i=0; i<orderNumbers.size();i++){
values.add(orderNumbers.get(i));
}
cellList.setRowData(values);
}
});
basically I'm trying to create a cellList which wants a List as input. I get an arrayList of orders from a DB query and then iterate through to pull out order numbers which I then put into a arrayList, which I then want to convert to a List to use in cellList. Can someone explain why I can't do this and what the proper method should be?
基本上我正在尝试创建一个需要列表作为输入的 cellList。我从数据库查询中获取订单的 arrayList,然后迭代以提取订单号,然后将其放入一个 arrayList,然后我想将其转换为 List 以在 cellList 中使用。有人可以解释为什么我不能这样做以及正确的方法应该是什么?
采纳答案by Alex
I don't understand the problem you have since an ArrayList
implements a List
with List
being an interface.
我不明白,因为一个你有问题,ArrayList
实现了一个List
具有List
作为一个接口。
You can change this line
你可以改变这一行
ArrayList<String> orderNumbers = new ArrayList<String>();
to:
到:
List<String> orderNumbers = new ArrayList<String>();
回答by Ted Hopp
An ArrayList<anything>
is already a List<anything>
, because ArrayList
implements the List
interface. No casting is necessary.
AnArrayList<anything>
已经是 a List<anything>
,因为ArrayList
实现了List
接口。不需要铸造。
回答by Dima
java.util.List
is an interface, and the ArrayList<E>
already implemented it, you don't have to cast anything
java.util.List
是一个接口,并且ArrayList<E>
已经实现了它,您不必强制转换任何内容
回答by Eric Jablow
An ArrayList
is a List
, so I'm not sure what might be happening. However, I do have a suspicion. Look at the imports at the top of the file. Perhaps you have the line atop the file:
AnArrayList
是 a List
,所以我不确定会发生什么。不过,我有一个怀疑。查看文件顶部的导入。也许您在文件顶部有一行:
import java.awt.List;
I have told my IDE not to use java.awt.List
as a suggestion for List
. However, you are still writing too much code. Consider Java's foreach
loops:
我已经告诉我的 IDE 不要java.awt.List
用作List
. 但是,您仍然编写了太多代码。考虑 Java 的foreach
循环:
@Override
public void onSuccess(ArrayList<Order> result) {
List<String> orderNumbers = new ArrayList<String>();
for (Order order: result) {
orderNumbers.add(Integer.toString(order.getOrderNumber()));
}
values.addAll(orderNumbers);
cellList.setRowData(values);
}
But you don't need the orderNumbers variable at all. Just use values.add instead of orderNumbers.add. And then, why are you using a List<String>
at all? Can you survive with a List<Integer>
and avoid the conversion?
但是您根本不需要 orderNumbers 变量。只需使用 values.add 而不是 orderNumbers.add。然后,你为什么要使用 a List<String>
?你能用 a 生存List<Integer>
并避免转换吗?
Whoops--you're using GWT. Does Google still advise programmers to not use interfaces so it doesn't have to generate every concrete implementation in Java?
糟糕——您正在使用 GWT。谷歌是否仍然建议程序员不要使用接口,这样它就不必用 Java 生成每个具体的实现?