将一个二维数组转换成一个二维ArrayList?

7

我有这段代码:

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

我需要将这个二维数组转换成一个二维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 },

我只是想将这个二维数组转换成一个二维ArrayList,任何帮助都将不胜感激!


请看接受的答案中的评论,为什么它会将数组向右移动一个位置? - SomeDude
4个回答

12

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

第二种情况 如果你不想将基本类型转换为引用类型:(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);
}

在情况2中,当使用list.add()方法时,原始的int类型会被隐式转换为Integer类型。 - Julian Rubin
回答已更新,我的意思是数组类型,我知道你说的是什么,在情况1中,如果你不将原始类型转换为引用类型,则 Arrays.asList() 不起作用。 - Bahramdun Adil
@HexxNine 我在想,如何将你的数组向右移动2行,2列?这不是你一开始的要求吗? - SomeDude

1
简而言之,对于1d原始int数组,在这种情况下必须使用 IntStream.of() 而不是 stream()。
int[][] input = new int[][]{{1, 3, 10}, {2, 4, 55}};

System.out.println(
            Arrays.stream(input)
                    .map(e -> IntStream.of(e).boxed().collect(Collectors.toList()))
                    .collect(Collectors.toList())
);
// [[1, 3, 10], [2, 4, 55]]

对于装箱的整数数组,请参见Daria Yu的答案。

1
如果您将原始类型 int 转换为引用类型 Integer,则可以使用流(stream):
public static void Two_Array_to_list_1() {
Integer[][] dataSet = new Integer[][] {{1, 2}, {3, 4}, {5, 6}};

List<List<Integer>> list =
    Arrays.stream(dataSet).map(Arrays::asList).collect(Collectors.toList());

System.out.println(list);

}

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

查看演示 此处

当您将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 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接