# 将2D数组的列拆分为一个单独的2D数组。

go评论48阅读模式

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

``````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));
``````

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 -&gt; IntStream.rangeClosed(startColumn, endColumn)
.map(j -&gt; 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

``````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 是不包括的。

> 这背后有一个故事。亚洲的[_珠算_][1]与罗马的 _算盘_ 相比旋转了90°。在我看来，这使它更易读，因为它用于阅读数字，而 _珠算_ 确实是一个很好的教育工具和有用的工具。

[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&#39;s matrix class.

&gt; There is a story behind this. The Asian [_soroban_][1] is 90&#176; 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

```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 &#39;1&#39; inclusive to &#39;3&#39; exclusive
.map(row -&gt; 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]
``````

• 本文由 发表于 2020年7月29日 02:29:33
• 转载请务必保留本文链接：https://go.coder-hub.com/63140585.html
• arrays
• java
• multidimensional-array
• sub-array

go 53

go 66

go 42

go 49