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

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!



Solution 1:[1]

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

}

Solution 2:[2]

For short, for 1d primitive int array, have to use IntStream.of() rather than stream() in this case

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]]

for boxed Integer array, see Daria Yu's answer

Solution 3:[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 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vikki
Solution 2
Solution 3