在java中,是否有一个预定义的方法通过给定的行数和列数来实现padarray

时间:2022-01-27 20:47:59

How to padarray in java that is add row and column to a existing array in front and back with a given number.

如何在java中使用padarray将行和列添加到给定数字前后的现有数组中。

For example :-

例如:-

let x =  1  2  3
         4  5  6
         7  8  9   

and now want to 2 rows and columns of zeros in this:

现在想要2行0列在这里

   x =  0 0 0 0 0 0 0 
        0 0 0 0 0 0 0
        0 0 1 2 3 0 0
        0 0 4 5 6 0 0
        0 0 7 8 9 0 0
        0 0 0 0 0 0 0
        0 0 0 0 0 0 0

So, i want to know that is there a existing method or way to do this in java like it is available in matlab using the predefined method called padarray(x,[r,c]).

因此,我想知道在java中是否存在一个现有的方法或方法,比如在matlab中使用名为padarray(x,[r,c])的预定义方法。

2 个解决方案

#1


4  

You can never add rows or columns to 2 dimensional arrays at all. Arrays are fixed size. You could use a dynamic data structure such as List<List<Integer>>.

你永远不可能向二维数组添加行或列。数组是固定的尺寸。您可以使用动态数据结构,如List >。

What you also can do is create a new array (that is bigger or smaller than your current one) using the Arrays.copyOf(int[] original, int newLength); method.

您还可以使用数组创建一个新的数组(比当前数组大或小)。copyOf(int[],int newLength);方法。

You array x is like:

你的数组x是这样的:

  int[][] x = new int[][]{{1,2,3}, {4,5,6}, {7,8,9}};

There is no one-liner (I know of) to transform it to your desired format. You have to create a method that creates a new 2 dimensional array and place your values at the correct indexes.

没有一行代码(据我所知)可以将其转换为所需的格式。您必须创建一个方法来创建一个新的二维数组,并将您的值放置在正确的索引处。

#2


0  

Here is some code that takes the 2D array you want padded, what you want it to be padded with, and how many pads to surround the array with (yours would be 2 because you want your array surrounded with 2 layers of 0's).

这里有一些代码,它使用你想要填充的2D数组,你想要填充的是什么,以及围绕数组的多少垫(你的垫层是2,因为你想让你的数组被2个0层包围)。

public static int[][] padArray(int[][] arr, int padWith, int numOfPads) {
    int[][] temp = new int[arr.length + numOfPads*2][arr[0].length + numOfPads*2];
    for (int i = 0; i < temp.length; i++) {
        for (int j = 0; j < temp[i].length; j++) {
            temp[i][j] = padWith;
        }
    }
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            temp[i+numOfPads][j+numOfPads] = arr[i][j];
        }
    }
    return temp;
}

#1


4  

You can never add rows or columns to 2 dimensional arrays at all. Arrays are fixed size. You could use a dynamic data structure such as List<List<Integer>>.

你永远不可能向二维数组添加行或列。数组是固定的尺寸。您可以使用动态数据结构,如List >。

What you also can do is create a new array (that is bigger or smaller than your current one) using the Arrays.copyOf(int[] original, int newLength); method.

您还可以使用数组创建一个新的数组(比当前数组大或小)。copyOf(int[],int newLength);方法。

You array x is like:

你的数组x是这样的:

  int[][] x = new int[][]{{1,2,3}, {4,5,6}, {7,8,9}};

There is no one-liner (I know of) to transform it to your desired format. You have to create a method that creates a new 2 dimensional array and place your values at the correct indexes.

没有一行代码(据我所知)可以将其转换为所需的格式。您必须创建一个方法来创建一个新的二维数组,并将您的值放置在正确的索引处。

#2


0  

Here is some code that takes the 2D array you want padded, what you want it to be padded with, and how many pads to surround the array with (yours would be 2 because you want your array surrounded with 2 layers of 0's).

这里有一些代码,它使用你想要填充的2D数组,你想要填充的是什么,以及围绕数组的多少垫(你的垫层是2,因为你想让你的数组被2个0层包围)。

public static int[][] padArray(int[][] arr, int padWith, int numOfPads) {
    int[][] temp = new int[arr.length + numOfPads*2][arr[0].length + numOfPads*2];
    for (int i = 0; i < temp.length; i++) {
        for (int j = 0; j < temp[i].length; j++) {
            temp[i][j] = padWith;
        }
    }
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            temp[i+numOfPads][j+numOfPads] = arr[i][j];
        }
    }
    return temp;
}