java 如何从JAVA中的二维数组获取二维子数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16362872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get 2D subarray from 2D array in JAVA?
提问by Somnath Kadam
Suppose I have 2D array as follow:
假设我有如下二维数组:
int[][] temp={
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}};
and i want to get sub-array start from X direction 1 to 2 and Y direction 1 to 2 i.e.
我想从 X 方向 1 到 2 和 Y 方向 1 到 2 开始子阵列,即
{6,7}
{10,11}
can anyone give me solution for above problem.
谁能给我解决上述问题的方法。
回答by Evgeniy Dorofeev
Here you are
这个给你
int[][] temp = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
int[][] a = new int[temp.length][];
for (int i = 0; i < temp.length; i++) {
a[i] = Arrays.copyOfRange(temp[i], 1, 3);
}
System.out.println(Arrays.deepToString(a));
output
输出
[[2, 3], [6, 7], [10, 11]]
answering your question in comment if we want to access only [[6, 7], [10, 11]]
如果我们只想访问 [[6, 7], [10, 11]],请在评论中回答您的问题
int[][] a = new int[2][];
for (int i = 1, j = 0; i < 3; i++, j++) {
a[j] = Arrays.copyOfRange(temp[i], 1, 3);
}
output
输出
[[6, 7], [10, 11]]
回答by Matt Hyde
As an example without using the Arrays class:
作为不使用 Arrays 类的示例:
int[][] temp = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
int[][] subArray = new int[temp.length][];
for (int i = 0; i < subArray.length; i++) {
subArray[i] = new int[2];
subArray[i][0] = temp[i][1];
subArray[i][1] = temp[i][2];
}
You can then access any part of subArray that you want. This will access the values [[6, 7], [10, 11]]:
然后,您可以访问所需的 subArray 的任何部分。这将访问值 [[6, 7], [10, 11]]:
for (int x = 1; x < 3; x++) {
System.out.println(subArray[x][0]);
System.out.println(subArray[x][1]);
}
[Additional] To address the modified question:
[附加] 要解决修改后的问题:
If you want to create a smaller array you can play around with the start and end points of the loop, and the indices accessed within the loop, for example this will create the array you ask for:
如果您想创建一个较小的数组,您可以使用循环的起点和终点以及循环内访问的索引,例如这将创建您要求的数组:
int[][] subArray = new int[2][];
for (int i = 1; i < temp.length; i++) {
subArray[i-1] = new int[2];
subArray[i-1][0] = temp[i][1];
subArray[i-1][1] = temp[i][2];
}
回答by Kcits
Just adding a little bit to the accepted answer.
只是在接受的答案中添加一点。
You can make a static method to generalize it for all 2D arrays with fixed width and height
您可以使用静态方法将其推广到所有具有固定宽度和高度的二维数组
public static char[][] get2DSubArray(char[][] origArray, int fromRow, int toRow, int fromColumn, int toColumn) {
char[][] subArray = new char[toColumn - fromColumn][];
for (int i = fromCol; i < toCol; i++) {
subArray[i - fromCol] = Arrays.copyOfRange(origArray[i], fromRow, toRow);
}
return subArray;
}
Hope this can help too!
希望这也能有所帮助!
回答by John
You could instantiate a new array and loop through the appropriate indices of the original array to initialize the subarray.
您可以实例化一个新数组并循环遍历原始数组的适当索引以初始化子数组。