英文:
Splitting columns of 2D array into a separate 2D array
问题
I would like to send certain columns of a 2D array to another method as a 2D array. For example, I have the following array:
1 2 6 4
2 5 3 9
2 5 1 3
I would like to send, let's say, the middle 2 columns to a method as the 2D array:
2 6
5 3
5 1
英文:
I would like to send certain columns of a 2D array to another method as a 2D array. For example, I have the following array:
1 2 6 4
2 5 3 9
2 5 1 3
I would like to send lets say the middle 2 columns to a method as the 2D array:
2 6
5 3
5 1
How would I split this in a way that I could clarify the amount of columns being split and where I want the split to start and end. My understanding is that System.arrayCopy()
would be an efficient way of performing this, however, I can only copy a 1D array with it.
答案1
得分: 2
尝试这个。
int[][] arr = new int[][]{
{1, 2, 6, 4},
{2, 5, 3, 9},
{2, 5, 1, 3}
};
int startColumn = 1;
int endColumn = 2;
int[][] result = Arrays.stream(arr)
.map(row -> IntStream.rangeClosed(startColumn, endColumn)
.map(j -> row[j]).toArray())
.toArray(int[][]::new);
System.out.println(Arrays.deepToString(res));
输出: [[2, 6], [5, 3], [5, 1]]
它对二维数组进行流处理,对于每一行,它通过选择从 startColumn
到 endColumn
(包括边界列)的列来构建一个一维数组,并将每个数组收集到一个二维数组中。
英文:
Try this.
int[][] arr = new int[][]{
{1, 2, 6, 4},
{2, 5, 3, 9},
{2, 5, 1, 3}
};
int startColumn = 1;
int endColumn = 2;
int[][] result = Arrays.stream(arr)
.map(row -> IntStream.rangeClosed(startColumn, endColumn)
.map(j -> row[j]).toArray())
.toArray(int[][]::new);
System.out.println(Arrays.deepToString(res));
Output: [[2, 6], [5, 3], [5, 1]]
It streams the 2D array and for each row, it builds a 1D array by picking the columns from startColumn
to endColumn
inclusive and collects every array in a 2D array.
答案2
得分: 2
有个教训在其中。
从一个二维数组中挑选几行更容易。
所以也许将列"表示"为行。
int[][] arr = {
{1, 2, 2},
{2, 5, 5},
{6, 3, 1},
{4, 9, 3}
};
int[][] sub = new int[] {
Arrays.copyOf(arr[1], 3),
Arrays.copyOf(arr[2], 3)
};
int[][] sharing = Arrays.copyOfRange(arr, 1, 3); // 3 是不包括的。
最后的 `sharing` 与 `arr` 共享行(列)。
一个可以创建自己的矩阵类。
> 这背后有一个故事。亚洲的[_珠算_][1]与罗马的 _算盘_ 相比旋转了90°。在我看来,这使它更易读,因为它用于阅读数字,而 _珠算_ 确实是一个很好的教育工具和有用的工具。
工具:[`Arrays.copyOfRange(T[],int,int)`][2]。
[1]: https://en.wikipedia.org/wiki/Soroban
[2]: https://docs.oracle.com/javase/10/docs/api/java/util/Arrays.html#copyOfRange(T%5B%5D,int,int)
<details>
<summary>英文:</summary>
There is a lesson in this.
It is easier to pick out a couple of rows of a two dimensional array.
So maybe _represent_ columns as rows.
int[][] arr = {
{1, 2, 2},
{2, 5, 5},
{6, 3, 1},
{4, 9, 3}
};
int[][] sub = new int[] {
Arrays.copyOf(arr[1], 3),
Arrays.copyOf(arr[2], 3)
};
int[][] sharing = Arrays.copyOfRange(arr, 1, 3); // 3 exclusive.
The last `sharing` shares the rows (columns) with `arr`.
One could make one's matrix class.
> There is a story behind this. The Asian [_soroban_][1] is 90° rotated with respect to the Roman _abacus_. This makes it more readable IMHO, as reading numbers, and the _soroban_ indeed is still a good pedagogical and useful tool.
Utilities: [`Arrays.copyOfRange(T[],int,int)`][2].
[1]: https://en.wikipedia.org/wiki/Soroban
[2]: https://docs.oracle.com/javase/10/docs/api/java/util/Arrays.html#copyOfRange(T%5B%5D,int,int)
</details>
# 答案3
**得分**: 1
要从现有的二维数组中获取一个二维子数组 - 您可以遍历此数组的行,对于每一行,使用 [`Arrays.stream(int[],int,int)`][1] 方法遍历*指定的范围*:
```java
int[][] arr1 = {
{1, 2, 6, 4},
{2, 5, 3, 9},
{2, 5, 1, 3}};
int[][] arr2 = Arrays.stream(arr1)
// 范围从 '1'(包括)到 '3'(不包括)
.map(row -> Arrays.stream(row, 1, 3).toArray())
.toArray(int[][]::new);
// 输出
Arrays.stream(arr2).map(Arrays::toString).forEach(System.out::println);
[2, 6]
[5, 3]
[5, 1]
英文:
To get a 2d sub-array from an existing 2d array - you can iterate over the rows of this array, and for each row, iterate over the specified range using the Arrays.stream(int[],int,int)
method:
int[][] arr1 = {
{1, 2, 6, 4},
{2, 5, 3, 9},
{2, 5, 1, 3}};
int[][] arr2 = Arrays.stream(arr1)
// range from '1' inclusive to '3' exclusive
.map(row -> Arrays.stream(row, 1, 3).toArray())
.toArray(int[][]::new);
// output
Arrays.stream(arr2).map(Arrays::toString).forEach(System.out::println);
[2, 6]
[5, 3]
[5, 1]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论