简体   繁体   中英

Splitting a Java 2D array (4x4) into smaller ones (2x2) and joining them again

This source code splits a 4x4 array into an array of 2x2... How can I restore the value of the 2x2 array into a 4x4 array?

public class testmatriks {
    private static Random generator = new Random();

    public static void main(String[] args) {
        double[][] mainArray = new double[4][4];
        double[][] subArray = new double[2][2];
        double[][] rubArray = new double[4][4];
        int value;
        for (int i = 0; i < mainArray.length; i++) {
            for (int j = 0; j < mainArray[0].length; j++) {
                value = generator.nextInt(255);
                mainArray[i][j] = value;
                System.out.print(mainArray[i][j] + "  ");
            }
            System.out.println("");
        }
        System.out.println("\n");
        System.out.println("pecah ke piksel 2x2 piksel");

        for (int row = 0; row < (mainArray.length); row += 2) {
            for (int column = 0; column < mainArray[0].length; column += 2) {
                for (int k = 0; k < 2; k++) {
                    for (int l = 0; l < 2; l++) {
                        subArray[k][l] = mainArray[row + k][column + l] * 2;
                        System.out.print(subArray[k][l] + " ");
                    }
                    System.out.println(" ");
                }
            }
        }
    }
}

这张照片说明了我想要的

This picture illustrates what I want.

This is what you initially have in the 4x4 array:

[a 1 | b 1 | c 1 | d 1 ]
[a 2 | b 2 | c 2 | d 2 ]
[a 3 | b 3 | c 3 | d 3 ]
[a 4 | b 4 | c 4 | d 4 ]

This is what will be in the 2x2 array you created:

[2 * c 3 | 2 * d 3 ]
[2 * c 4 | 2 * d 4 ]

When you say to "restore", it is not clear where to place the values in this 4x4 array, so I will just go with the places in the 4x4 used to create the 2x2:

The code:

for (int r = 0; r < 2; r++) {
    for (int c = 0; c < 2; c++) {
        mainArray[r + 2][c + 2] = subArray[r][c];
    }
}

This should be the content after the code runs:

[a 1 | b 1 | c 1 | d 1 ]
[a 2 | b 2 | c 2 | d 2 ]
[a 3 | b 3 | c 3 + c 3 | d 3 + d 3 ]
[a 4 | b 4 | c 4 + c 4 | d 4 + d 4 ]


For splitting the array into smaller chunks, here is what I came up with:

/**
* @param larger the larger array to be split into sub arrays of size chunksize
* @param chunksize the size of each sub array
* @precond chunksize > 0 && larger != null
* @throws ArrayIndexOutOfBoundsException, NullPointerException
*/
public static List<double[][]> chunks(double [][]larger, int chunksize) throws 
    ArrayIndexOutOfBoundsException, NullPointerException  {
    if (chunksize <= 0)
        throw new ArrayIndexOutOfBoundsException("Chunks must be atleast 1x1");
    int size = larger.length / chunksize * (larger[0].length / chunksize);
    List<double[][]> subArrays = new ArrayList<>();

    for (int c = 0; c < size; c++) {
        double[][] sub = new double[chunksize][chunksize];
        int startx = (chunksize * (c / chunksize)) % larger.length;
        int starty = (chunksize * c) % larger[0].length;

        if (starty + chunksize > larger[0].length) {
            starty = 0;
        }

        for (int row = 0; row < chunksize; row++) {
            for (int col = 0; col < chunksize; col++) {
                sub[row][col] = larger[startx + row][col + starty];
            }
        }
        subArrays.add(sub);
    }

    return subArrays;
}

I haven't tested it yet, but you can easily test it with the code you already have.

Sample usage: http://ideone.com/D5Tdgs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM