将二维数组传输到二维ArrayList?

时间:2016-03-03 02:34:15

标签: java arrays arraylist

我有这段代码:

int[][] pattern = new int[][]{
        { 1, 1, 1, 1, 1, 1, 1 },
        { 1, 2, 0, 0, 0, 2, 1 },
        { 1, 0, 3, 0, 3, 0, 1 },
        { 1, 0, 0, 4, 0, 0, 1 },
        { 1, 0, 3, 0, 3, 0, 1 },
        { 1, 2, 0, 0, 0, 2, 1 },
        { 1, 1, 1, 1, 1, 1, 1 },
};

我需要将这个2d数组放入2d ArrayList中,这样我就可以通过添加行和列来移动模式来操作它。例如,当我的方法调用2行和2列的移位时,我将能够将模式移动到这样的:

        { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
        { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
        { 0, 0, 1, 1, 1, 1, 1, 1, 1 },
        { 0, 0, 1, 2, 0, 0, 0, 2, 1 },
        { 0, 0, 1, 0, 3, 0, 3, 0, 1 },
        { 0, 0, 1, 0, 0, 4, 0, 0, 1 },
        { 0, 0, 1, 0, 3, 0, 3, 0, 1 },
        { 0, 0, 1, 2, 0, 0, 0, 2, 1 },
        { 0, 0, 1, 1, 1, 1, 1, 1, 1 },

我只是想将2d阵列变成2d Arraylist,任何帮助都将不胜感激!

4 个答案:

答案 0 :(得分:6)

案例1 它很短,但需要根据int Integer到Arrays.asList();) >

Integer[][] pattern = new Integer[][]{
        { 1, 1, 1, 1, 1, 1, 1 },
        { 1, 2, 0, 0, 0, 2, 1 },
        { 1, 0, 3, 0, 3, 0, 1 },
        { 1, 0, 0, 4, 0, 0, 1 },
        { 1, 0, 3, 0, 3, 0, 1 },
        { 1, 2, 0, 0, 0, 2, 1 },
        { 1, 1, 1, 1, 1, 1, 1 },
};
List<List<Integer>> lists = new ArrayList<>();
for (Integer[] ints : pattern) {
    lists.add(Arrays.asList(ints));
}

案例2 如果您不想将基元类型转换为引用类型:(int[][] pattern = new int[][]Integer[][] pattern = new Integer[][]

List<List<Integer>> lists = new ArrayList<>();
for (int[] ints : pattern) {
    List<Integer> list = new ArrayList<>();
    for (int i : ints) {
        list.add(i);
    }
    lists.add(list);
}

答案 1 :(得分:1)

如果您将原始类型 int 隐式引用为 Integer 类型,则可以使用流:

localhost:9000

答案 2 :(得分:0)

您可以这样做:

public static List<Integer[]> twoDArrayList(int shift, int[][] input)
{

    List<Integer[]> output = new ArrayList<Integer[]>();
    if( input.length == 0 ) return null;
    int columnlength = input.length;
    int rowlength = input[0].length;
    if (columnlength != rowlength) return null;

    int padsize = shift;
    for( int i = 0; i < padsize; i++ )
    {
        Integer[] zeroes = new Integer[shift+columnlength];

        for( int j = 0; j < shift+columnlength; j++)
        {
            zeroes[j] = 0;
        }
        output.add( zeroes );
    }

    for( int i = 0; i < columnlength; i++ )
    {
        int[] row = input[i];
        int[] zeroes = new int[shift];
        List<Integer> temp = new ArrayList<Integer>();
        for( int j = 0; j < shift; j++)
        {
            temp.add(0);
        }
        for( int k = 0; k < row.length; k++)
        {
            temp.add(row[k]);
        }
        output.add(temp.toArray(new Integer[]{}));
    }

    return output;
}

请参阅演示here

当您将shift提供为2时:输出将如下所示:

Running Shifting array...
Array no. 0 in the list is : 0 0 0 0 0 0 0 0 0 
Array no. 1 in the list is : 0 0 0 0 0 0 0 0 0 
Array no. 2 in the list is : 0 0 1 1 1 1 1 1 1 
Array no. 3 in the list is : 0 0 1 2 0 0 0 2 1 
Array no. 4 in the list is : 0 0 1 0 3 0 3 0 1 
Array no. 5 in the list is : 0 0 1 0 0 4 0 0 1 
Array no. 6 in the list is : 0 0 1 0 3 0 3 0 1 
Array no. 7 in the list is : 0 0 1 2 0 0 0 2 1 
Array no. 8 in the list is : 0 0 1 1 1 1 1 1 1 

当您提供3时,您的输出如下:

Running Shifting array...
Array no. 0 in the list is : 0 0 0 0 0 0 0 0 0 0 
Array no. 1 in the list is : 0 0 0 0 0 0 0 0 0 0 
Array no. 2 in the list is : 0 0 0 0 0 0 0 0 0 0 
Array no. 3 in the list is : 0 0 0 1 1 1 1 1 1 1 
Array no. 4 in the list is : 0 0 0 1 2 0 0 0 2 1 
Array no. 5 in the list is : 0 0 0 1 0 3 0 3 0 1 
Array no. 6 in the list is : 0 0 0 1 0 0 4 0 0 1 
Array no. 7 in the list is : 0 0 0 1 0 3 0 3 0 1 
Array no. 8 in the list is : 0 0 0 1 2 0 0 0 2 1 
Array no. 9 in the list is : 0 0 0 1 1 1 1 1 1 1 

答案 3 :(得分:0)

简而言之,对于一维原始 int 数组,在这种情况下必须使用 IntStream.of() 而不是 stream()

groupby

对于盒装整数数组,请参阅 Daria Yu 的回答