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

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

I have a variable like that:

List frameList =  new ArrayList();

/* Double elements has added to frameList */

H

7条回答
  •  被撕碎了的回忆
    2020-11-28 13:44

    As per your question,

    List frameList =  new ArrayList();
    
    1. First you have to convert List to Double[] by using

      Double[] array = frameList.toArray(new Double[frameList.size()]);
      
    2. Next you can convert Double[] to double[] using

      double[] doubleArray = ArrayUtils.toPrimitive(array);
      

    You can directly use it in one line:

    double[] array = ArrayUtils.toPrimitive(frameList.toArray(new Double[frameList.size()]));
    

提交回复
热议问题