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

问题描述 投票:0回答:2

我有 :

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

我要转换为`

int[][] adjL = new int[?][?];

那可能吗?我该怎么用数组大小​​替换?

java arrays arraylist
2个回答
3
投票

不简单,但这可以帮助你:

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);
    }
}

对于像这样的输入:

adjList.add(new ArrayList<>(Arrays.asList(1, 2, 3, 4)));
adjList.add(new ArrayList<>(Arrays.asList(1, 2, 3)));
...    
System.out.println(Arrays.deepToString(result));

输出 :

[[1, 2, 3, 4], [1, 2, 3]]

2
投票

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

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

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

它显示了将ArrayList<ArrayList<Integer>>转换为int[][]数组的情况,但也包含showingThatThisIsFarMoreGeneric方法。

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Vector;

public class ToIntArray2D
{
    public static void main(String[] args)
    {
        ArrayList<ArrayList<Integer>> adjList = createTestLists();
        int[][] result = toIntArray2D(adjList);
        System.out.println(Arrays.deepToString(result));
    }

    private static int[][] toIntArray2D(
        Collection<? extends Collection<? extends Number>> collections)
    {
        return collections.stream()
            .map(c -> toIntArray(c))
            .<int[]> toArray(int[][]::new);
    }

    private static int[] toIntArray(
        Collection<? extends Number> collection)
    {
        return collection.stream().mapToInt(Number::intValue).toArray();
    }

    private static void showingThatThisIsFarMoreGeneric()
    {
        ArrayList<ArrayList<Integer>> exampleA = null;
        List<List<Integer>> exampleB = null;
        Set<Vector<Float>> exampleC = null;
        LinkedList<HashSet<? extends BigInteger>> exampleD = null;
        LinkedList<? extends HashSet<? extends BigDecimal>> exampleE = null;

        toIntArray2D(exampleA); // Works
        toIntArray2D(exampleB); // Works
        toIntArray2D(exampleC); // Works
        toIntArray2D(exampleD); // Works
        toIntArray2D(exampleE); // Works
    }

    private static ArrayList<ArrayList<Integer>> createTestLists()
    {
        ArrayList<ArrayList<Integer>> adjList =
            new ArrayList<ArrayList<Integer>>();

        ArrayList<Integer> adjList0 = new ArrayList<Integer>();
        adjList0.add(0);
        adjList0.add(1);
        adjList0.add(2);
        adjList.add(adjList0);

        ArrayList<Integer> adjList1 = new ArrayList<Integer>();
        adjList1.add(3);
        adjList1.add(4);
        adjList.add(adjList1);

        ArrayList<Integer> adjList2 = new ArrayList<Integer>();
        adjList2.add(5);
        adjList2.add(6);
        adjList2.add(7);
        adjList2.add(8);
        adjList.add(adjList2);
        return adjList;
    }

}

旁注:考虑到你应该usually program to an interface这样的事实

ArrayList<ArrayList<Integer>> adjList;

绝对不应该出现在实际代码中。它应该是一个

List<List<Integer>> adjList;
© www.soinside.com 2019 - 2024. All rights reserved.