Transfer a Two-Dimensional array to Two-Dimensional ArrayList?

谁说胖子不能爱 提交于 2020-03-18 12:52:06

问题


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:

        { 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!


回答1:


Case 1 It is short, but need to covert the primitive type to reference type (int to Integer) as needed for 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));
}

Case 2 If you don't want to covert the primitive type to reference type: (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:


If you covert the primitive type int to reference type Integer, you can use 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);

}



回答3:


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:

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 :

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 


来源:https://stackoverflow.com/questions/35762109/transfer-a-two-dimensional-array-to-two-dimensional-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!