简体   繁体   English

将二维双精度数组转换为二维双精度数组列表

[英]Convert a 2d double array to 2d double arraylist

I have a 2D Double array that is expanding and I am not sure of its final size therefore I want to convert it to a 2d arraylist but not sure how to do this I tried to define the array list first and use addAll(Array) method but it doesn't work. 我有一个正在扩展的2D Double数组,我不确定其最终大小,因此我想将其转换为2d arraylist,但不确定如何执行此操作,因此我尝试先定义数组列表并使用addAll(Array)方法但这不起作用。 Any suggestion? 有什么建议吗?

I just wrote this code I think it will work fine but I am looking for more efficient way 我只是写了这段代码,我认为它可以正常工作,但我正在寻找更有效的方法

public class trial {    

 public static void main(String[] arg){

    double[][] Solutions_to_arrange={{1,5,2,8,4,70,50,80},{3,7,4,2,6,60,30,70},{8,1,6,4,2,10,40,60}};
    ArrayList<ArrayList<Double>> Solutions_to_arrange_A=new ArrayList<ArrayList<Double>>();
    for(int i=0;i<Solutions_to_arrange.length;i++){
        Solutions_to_arrange_A.add(new ArrayList<Double>());
        for(int j=0;j<Solutions_to_arrange[0].length;j++){
            Solutions_to_arrange_A.get(i).add(Solutions_to_arrange[i][j]);
        }
    }

    }
}

Also I have question on putting the statement 我也有关于发表声明的问题

Solutions_to_arrange_A.add(new ArrayList<Double>());

Should I put it within the loop or its sufficient to call it just once outside the loop 我应该将其放在循环中还是足以在循环外调用一次

try it: 试试吧:

    Double[][] matriz = {{1d,5d,2d,8d,4d,70d,50d,80d},{3d,7d,4d,2d,6d,60d,30d,70d},{8d,1d,6d,4d,2d,10d,40d,60d}};

    List<List> result = new ArrayList<>();
    for(Double[] array : matriz){
        result.add( Arrays.asList(array) );
    }

If we use primitive types in "Arrays.asList" we will get a list with one primitive array as its unique item. 如果在“ Arrays.asList”中使用原始类型,我们将获得一个列表,其中一个原始数组作为其唯一项。 With object type it will works fine. 使用对象类型,它将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM