How to cast from List to double[] in Java?

前端 未结 7 555
野性不改
野性不改 2020-11-28 13:18

I have a variable like that:

List frameList =  new ArrayList();

/* Double elements has added to frameList */

H

7条回答
  •  猫巷女王i
    2020-11-28 13:52

    You can use primitive collections from Eclipse Collections and avoid boxing altogether.

    DoubleList frameList = DoubleLists.mutable.empty();
    double[] arr = frameList.toArray();
    

    If you can't or don't want to initialize a DoubleList:

    List frames = new ArrayList<>();
    double[] arr = ListAdapter.adapt(frames).asLazy().collectDouble(each -> each).toArray();
    

    Note: I am a contributor to Eclipse Collections.

提交回复
热议问题