Dee*_*Jha 3 java arrays multidimensional-array
如何在java中填充数组,即使用给定数字将行和列添加到前后的现有数组中。
例如 :-
let x = 1 2 3
4 5 6
7 8 9
Run Code Online (Sandbox Code Playgroud)
现在想要 2 行和列的零:
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
Run Code Online (Sandbox Code Playgroud)
所以,我想知道是否有一种现有的方法或方法可以在 Java 中执行此操作,就像在 matlab 中使用名为padarray(x,[r,c]).
你永远不能在二维数组中添加行或列。数组是固定大小的。您可以使用动态数据结构,例如List<List<Integer>>.
您还可以使用该Arrays.copyOf(int[] original, int newLength);方法创建一个新数组(比当前数组更大或更小)。
你的数组x是这样的:
int[][] x = new int[][]{{1,2,3}, {4,5,6}, {7,8,9}};
Run Code Online (Sandbox Code Playgroud)
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.
| 归档时间: |
|
| 查看次数: |
2292 次 |
| 最近记录: |