将二维数组传输到二维数组列表?

时间:2022-07-05 21:19:38

I have this piece of code:

我有这段代码:

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

I need to get this 2d array into a 2d ArrayList so i can manipulate it by adding rows and columns to move the pattern around. For example when my method calls for a shift of 2 rows and 2 columns i will be able to move the pattern to something like this:

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

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

I'm just looking to get the 2d array into a 2d Arraylist any help will be greatly appreciated!

我只是想把二维数组变成二维数组列表,任何帮助都将非常感谢!

2 个解决方案

#1


5  

Case 1 It is short, but need to covert the primitive type to reference type (int to Integer) as needed for Arrays.asList();

Case 1它很短,但是需要根据array . aslist()的需要将基元类型转换为引用类型(int to Integer);

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

Case 2 If you don't want to covert the primitive type to reference type: (int[][] pattern = new int[][] to Integer[][] pattern = new Integer[][])

情形2如果您不想将原语类型转换为引用类型:(int[][] pattern = new int[][] to 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


0  

You can do something like this:

你可以这样做:

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

See demo here

看到演示

When you supply shift as 2 : The output will look like:

当你供给位移为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 

When you supply 3 , your output looks like :

当你供给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 

#1


5  

Case 1 It is short, but need to covert the primitive type to reference type (int to Integer) as needed for Arrays.asList();

Case 1它很短,但是需要根据array . aslist()的需要将基元类型转换为引用类型(int to Integer);

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

Case 2 If you don't want to covert the primitive type to reference type: (int[][] pattern = new int[][] to Integer[][] pattern = new Integer[][])

情形2如果您不想将原语类型转换为引用类型:(int[][] pattern = new int[][] to 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


0  

You can do something like this:

你可以这样做:

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

See demo here

看到演示

When you supply shift as 2 : The output will look like:

当你供给位移为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 

When you supply 3 , your output looks like :

当你供给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