java 将双数组列表转换为双数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15555391/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:02:40  来源:igfitidea点击:

Converting a Double Array list to a double array

javaarraysarraylist

提问by sam

I am wanting to convert my double ArrayList to a double array but I am unsure how. Here is my current code listing :

我想将我的双 ArrayList 转换为双数组,但我不确定如何。这是我当前的代码清单:

ArrayList<Double> aL= new ArrayList<Double>();  
double x[] = new double[aL.size()];
aL.add(10.0);  
aL.add(5.0);  
aL.add(2.0);
for(int i =0; i < x.length; i++){
//some code
}

But then I am not sure where to go and I am unable to find something on the internet/ in books to give me any insight. Thanks.

但是后来我不知道该去哪里,而且我无法在互联网/书中找到一些东西来给我任何见解。谢谢。

采纳答案by Gilbert Le Blanc

First, you need to create your ArrayList. Then you need to create your array.

首先,您需要创建您的 ArrayList。然后你需要创建你的数组。

List<Double> aL= new ArrayList<Double>();  
aL.add(10.0D);  
aL.add(5.0D);  
aL.add(2.0D);

double x[] = new double[aL.size()];
for(int i = 0; i < x.length; i++){
     x[i] = al.get(i);
}

You can use an ArrayList method to copy the ArrayList to an array.

您可以使用 ArrayList 方法将 ArrayList 复制到数组。

List<Double> aL= new ArrayList<Double>();  
aL.add(10.0D);  
aL.add(5.0D);  
aL.add(2.0D);

double x[] = aL.toArray();

回答by Sudhanshu Umalkar

You can use Collection API to convert list to array. Try uL.toArray();

您可以使用 Collection API 将列表转换为数组。试试 uL.toArray();