Getting back primitive array after insertion into an ArrayList of primitive arrays in Java

后端 未结 4 1407
日久生厌
日久生厌 2020-12-18 17:42
List x = new ArrayList();
x.add(new double[]={1,2,3,4,54,6});  

elements 1,2,3,4,54,6 are added to x



        
4条回答
  •  天命终不由人
    2020-12-18 18:25

    If you just want your list to store the doubles instead of arrays of doubles, change what the list is storing

    List doubleList = new ArrayList();
    

    Then you can add the array as a list to your array list which will mean the list is storing the values rather than the array. This will give you the behaviour that get(0) will give you 1 rather than the array address

    Double[] doubleArray = {1.0, 2.0, 3.0, 4.0, 54.0, 6.0 };
    doubleList.addAll(Arrays.asList(doubleArray));
    doubleList.get(0); //gives 1.0
    

提交回复
热议问题