简体   繁体   English

用Java扩展2d数组/矩阵的大小

[英]Expanding size of 2d Array/ Matrix in Java

I have a 2x2 Matrix stored in a 2D Array in java 我在Java的2D数组中存储了2x2矩阵

int pixels [][] = new int[2][2];

which stores the folowing data in this way 它以这种方式存储以下数据

{0 2}
{0 2}

I am looking for a way for me to expand this matrix for example to a 8x8 Matrix so the result would be something like this: 我正在寻找一种方法将该矩阵扩展为例如8x8矩阵,因此结果将是这样的:

{0  0  0  0  2  2  2  2}
{0  0  0  0  2  2  2  2}
{0  0  0  0  2  2  2  2}
{0  0  0  0  2  2  2  2}
{0  0  0  0  2  2  2  2}
{0  0  0  0  2  2  2  2}
{0  0  0  0  2  2  2  2}
{0  0  0  0  2  2  2  2}

Any idea how this could be done using Java? 知道如何使用Java做到这一点吗?

This is what I've tried so far: 到目前为止,这是我尝试过的:

int amountToExpand = finalImage.length/2;
     for(int i = 0; i<finalImage.length; i++){
         for(int j = 0; j<finalImage.length; j++){
             if(i < amountToExpand+1){
                 finalImage[i][j]=pixels[][];
             }
         }
     }

The key insight is that the cell (i,j) in the final image contains the value of the cell (i/amountToExpand, j/amountToExpand) in the previous image. 关键的见解是最终图像中的单元格(i,j)包含前一图像中单元格的值(i / amountToExpand,j / amountToExpand)。

This code will work well if the ratio amountToExpand is an integer. 如果比率totalToExpand为整数,则此代码将运行良好。

int amountToExpand = finalImage.length / pixels.length;
        for (int i = 0; i < finalImage.length; i++) {
            for (int j = 0; j < finalImage[i].length; j++) {
                finalImage[i][j] = pixels[i / amountToExpand][j / amountToExpand];
            }
        }

Here you go... expandArray for the expansion and outputArray to display the results of an array for reference. 在这里,您可以... expandArray用于扩展,outputArray用于显示数组的结果以供参考。

public class Test {
    public static void main(String[] args) {
        try {
            int[][] pixels1 = new int[2][2];
            pixels1[0][0] = 1;
            pixels1[0][1] = 2;
            pixels1[1][0] = 3;
            pixels1[1][1] = 4;

            int[][] pixels2 = expandArray(pixels1, 4);

            outputArray(pixels1);
            outputArray(pixels2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static int[][] expandArray(int[][] arr, int factor) {
        int[][] output = new int[arr.length * factor][arr[0].length * factor];
        for (int i = 0; i < output.length; i++) {
            for (int j = 0; j < output[i].length; j++) {
                int orgRow = i / ((arr.length - 1) * factor);
                int orgCol = j / ((arr[0].length - 1) * factor);
                output[i][j] = arr[orgRow][orgCol];
            }
        }

        return output;
    }

    private static void outputArray(int[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }

            System.out.println();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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