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

huangapple go评论73阅读模式
英文:

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]]

它对二维数组进行流处理,对于每一行,它通过选择从 startColumnendColumn(包括边界列)的列来构建一个一维数组,并将每个数组收集到一个二维数组中。

英文:

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&#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

要从现有的二维数组中获取一个二维子数组 - 您可以遍历此数组的行对于每一行使用 [`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 &#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]

huangapple
  • 本文由 发表于 2020年7月29日 02:29:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63140585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定