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

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

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. 1 2 6 4
  2. 2 5 3 9
  3. 2 5 1 3

I would like to send, let's say, the middle 2 columns to a method as the 2D array:

  1. 2 6
  2. 5 3
  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. 1 2 6 4
  2. 2 5 3 9
  3. 2 5 1 3

I would like to send lets say the middle 2 columns to a method as the 2D array:

  1. 2 6
  2. 5 3
  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

尝试这个。

  1. int[][] arr = new int[][]{
  2. {1, 2, 6, 4},
  3. {2, 5, 3, 9},
  4. {2, 5, 1, 3}
  5. };
  6. int startColumn = 1;
  7. int endColumn = 2;
  8. int[][] result = Arrays.stream(arr)
  9. .map(row -> IntStream.rangeClosed(startColumn, endColumn)
  10. .map(j -> row[j]).toArray())
  11. .toArray(int[][]::new);
  12. System.out.println(Arrays.deepToString(res));

输出: [[2, 6], [5, 3], [5, 1]]

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

英文:

Try this.

  1. int[][] arr = new int[][]{
  2. {1, 2, 6, 4},
  3. {2, 5, 3, 9},
  4. {2, 5, 1, 3}
  5. };
  6. int startColumn = 1;
  7. int endColumn = 2;
  8. int[][] result = Arrays.stream(arr)
  9. .map(row -> IntStream.rangeClosed(startColumn, endColumn)
  10. .map(j -> row[j]).toArray())
  11. .toArray(int[][]::new);
  12. 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

有个教训在其中。

从一个二维数组中挑选几行更容易。
所以也许将列"表示"为行。

  1. int[][] arr = {
  2. {1, 2, 2},
  3. {2, 5, 5},
  4. {6, 3, 1},
  5. {4, 9, 3}
  6. };
  7. int[][] sub = new int[] {
  8. Arrays.copyOf(arr[1], 3),
  9. Arrays.copyOf(arr[2], 3)
  10. };
  11. int[][] sharing = Arrays.copyOfRange(arr, 1, 3); // 3 是不包括的。
  12. 最后的 `sharing` `arr` 共享行)。
  13. 一个可以创建自己的矩阵类
  14. > 这背后有一个故事亚洲的[_珠算_][1]与罗马的 _算盘_ 相比旋转了90°。在我看来这使它更易读因为它用于阅读数字 _珠算_ 确实是一个很好的教育工具和有用的工具
  15. 工具[`Arrays.copyOfRange(T[],int,int)`][2]
  16. [1]: https://en.wikipedia.org/wiki/Soroban
  17. [2]: https://docs.oracle.com/javase/10/docs/api/java/util/Arrays.html#copyOfRange(T%5B%5D,int,int)
  18. <details>
  19. <summary>英文:</summary>
  20. There is a lesson in this.
  21. It is easier to pick out a couple of rows of a two dimensional array.
  22. So maybe _represent_ columns as rows.
  23. int[][] arr = {
  24. {1, 2, 2},
  25. {2, 5, 5},
  26. {6, 3, 1},
  27. {4, 9, 3}
  28. };
  29. int[][] sub = new int[] {
  30. Arrays.copyOf(arr[1], 3),
  31. Arrays.copyOf(arr[2], 3)
  32. };
  33. int[][] sharing = Arrays.copyOfRange(arr, 1, 3); // 3 exclusive.
  34. The last `sharing` shares the rows (columns) with `arr`.
  35. One could make one&#39;s matrix class.
  36. &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.
  37. Utilities: [`Arrays.copyOfRange(T[],int,int)`][2].
  38. [1]: https://en.wikipedia.org/wiki/Soroban
  39. [2]: https://docs.oracle.com/javase/10/docs/api/java/util/Arrays.html#copyOfRange(T%5B%5D,int,int)
  40. </details>
  41. # 答案3
  42. **得分**: 1
  43. 要从现有的二维数组中获取一个二维子数组 - 您可以遍历此数组的行对于每一行使用 [`Arrays.stream(int[],int,int)`][1] 方法遍历*指定的范围*
  44. ```java
  45. int[][] arr1 = {
  46. {1, 2, 6, 4},
  47. {2, 5, 3, 9},
  48. {2, 5, 1, 3}};
  1. int[][] arr2 = Arrays.stream(arr1)
  2. // 范围从 '1'(包括)到 '3'(不包括)
  3. .map(row -> Arrays.stream(row, 1, 3).toArray())
  4. .toArray(int[][]::new);
  1. // 输出
  2. Arrays.stream(arr2).map(Arrays::toString).forEach(System.out::println);
  1. [2, 6]
  2. [5, 3]
  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:

  1. int[][] arr1 = {
  2. {1, 2, 6, 4},
  3. {2, 5, 3, 9},
  4. {2, 5, 1, 3}};
  1. int[][] arr2 = Arrays.stream(arr1)
  2. // range from &#39;1&#39; inclusive to &#39;3&#39; exclusive
  3. .map(row -&gt; Arrays.stream(row, 1, 3).toArray())
  4. .toArray(int[][]::new);
  1. // output
  2. Arrays.stream(arr2).map(Arrays::toString).forEach(System.out::println);
  1. [2, 6]
  2. [5, 3]
  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:

确定