Java 如何从ArrayList的ArrayList中获取二维数组?

Java 如何从ArrayList的ArrayList中获取二维数组?,java,arrays,arraylist,Java,Arrays,Arraylist,我有: ArrayList<ArrayList<Integer>> adjList = new ArrayList<ArrayList<Integer>>(); 可能吗?我应该用?替换阵列大小中的?什么?这并不简单,但这会帮助您: ArrayList<ArrayList<Integer>> adjList = new ArrayList<>(); Integer[][] result = new Intege

我有:

ArrayList<ArrayList<Integer>> adjList = new ArrayList<ArrayList<Integer>>();

可能吗?我应该用?

替换阵列大小中的
什么?这并不简单,但这会帮助您:

ArrayList<ArrayList<Integer>> adjList = new ArrayList<>();

Integer[][] result = new Integer[adjList.size()][];//create your array of arrays
for (int i = 0; i < adjList.size(); i++) {
    //foreach array you have to inisialize it with the correct size
    result[i] = new Integer[adjList.get(i).size()];

    //the fill the array with the value of your list
    for(int j = 0; j < adjList.get(i).size(); j++){
        result[i][j] = adjList.get(i).get(j);
    }
}

不简单,但这应该能帮助您:

ArrayList<ArrayList<Integer>> adjList = new ArrayList<>();

Integer[][] result = new Integer[adjList.size()][];//create your array of arrays
for (int i = 0; i < adjList.size(); i++) {
    //foreach array you have to inisialize it with the correct size
    result[i] = new Integer[adjList.get(i).size()];

    //the fill the array with the value of your list
    for(int j = 0; j < adjList.get(i).size(); j++){
        result[i][j] = adjList.get(i).get(j);
    }
}

问题基本上得到了回答,尽管建议的解决方案返回的是一个
整数[][]]
,而不是问题中要求的
int[][]]
,但这可能是一个细节

然而,将“数字集合”转换为2D基本数组的任务是一项非常通用的任务,因此,可以非常通用地编写它

这是另一个使用流的解决方案,可以应用于任意数字的任意集合。它不一定比另一个解决方案“更好”,但也可以作为其他应用案例的参考

它显示了转换
ArrayList的情况,如

ArrayList<ArrayList<Integer>> adjList;
arraylistadjlist;
无论如何都不应该在实际代码中出现。它应该是一个

List<List<Integer>> adjList;
List;

问题基本上得到了回答,尽管建议的解决方案返回的是一个
整数[][]
,而不是问题中要求的
int[][]
,但这可能是一个细节

然而,将“数字集合”转换为2D基本数组的任务是一项非常通用的任务,因此,可以非常通用地编写它

这是另一个使用流的解决方案,可以应用于任意数字的任意集合。它不一定比另一个解决方案“更好”,但也可以作为其他应用案例的参考

它显示了转换
ArrayList的情况,如

ArrayList<ArrayList<Integer>> adjList;
arraylistadjlist;
无论如何都不应该在实际代码中出现。它应该是一个

List<List<Integer>> adjList;
List;

你试过什么吗?@OliverCharlesworth噢,对不起,我忘了添加我试过的步骤。我试过使用adjList.toArray();但它只返回一维数组。我想尝试使用循环进行传输,但我不知道如何配置大小。你尝试过什么吗?@OliverCharlesworth,对不起,我忘记添加我尝试过的步骤。我试过使用adjList.toArray();但它只返回一维数组。我想尝试使用循环传输,但我不知道如何配置大小。非常感谢。我发现我错过了这个步骤
result[I]=newinteger[adjList.get(I).size()]我没有得到正确的尺码。非常感谢。我发现我错过了这个步骤
result[I]=newinteger[adjList.get(I).size()]我没有得到正确的尺寸。
List<List<Integer>> adjList;