如何将 java ArrayList<Double> 转换为等效的 double[]

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

How do I convert a java ArrayList<Double> to the equivalent double[]

java

提问by deltanovember

Possible Duplicate:
How to cast from List<Double> to double[] in Java?

可能的重复:
如何在 Java 中从 List<Double> 转换为 double[]?

So far the only method I can get working is to loop through the entire array. I was hoping for something along the lines of:

到目前为止,我可以使用的唯一方法是遍历整个数组。我希望有以下几点:

ArrayList<Double> myArrayList = ... // initiaize
double[] myArray = ... // magic line here

回答by lschin

Use Google Guava: Doubles.toArray

使用谷歌番石榴Doubles.toArray

myArray = Doubles.toArray(myArrayList);

回答by Yanick Rochon

You cannot go from ArrayList<Double>to double[]with a single call using the sandard Java API. However, take a look at the ArrayUtilsclass available from Apache's site.

你不能从去ArrayList<Double>double[]使用的sandard Java API的一个调用。但是,请查看Apache 站点上提供的ArrayUtils类。

On a side note, if you need to change your ArrayListinto a primitive array, perhaps your problem is somewhere else. If you explain further exactly what you are trying to achieve, we could provide you better help than a simple API hack.

附带说明一下,如果您需要将您ArrayList的数组更改为原始数组,那么您的问题可能出在其他地方。如果您进一步准确地解释您要实现的目标,我们可以为您提供比简单的 API hack 更好的帮助。