在二维数组中插入列到指定位置。

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

Insert column in desired location of 2D array

问题

我想选择数组中的一列,比如第2列。我希望将这一列插入到二维数组的特定位置,比如第4列。

例如:

  1. 1 3 5 5 2
  2. 2 4 6 2 1
  3. 3 6 9 1 1

期望的输出将是:

  1. 1 5 5 3 2
  2. 2 6 2 4 1
  3. 3 9 1 6 1

我知道我可以使用以下代码循环,直到逐个交换每一列,直到列在所需位置为止:

  1. for (int[] array1 : array) {
  2. int temp = array1[col1];
  3. array1[col1] = array1[col2];
  4. array1[col2] = temp;
  5. }

然而,如果我使用宽度为30列的大矩阵,这将非常低效。是否有一种方法可以在二维数组中的任何位置插入列,而不必迭代每次交换直到它在正确的位置?

英文:

I'm looking to choose a column of my array, lets say column 2. I want this column to be inserted at a specific location in the 2D array, lets say column 4.

For example:

  1. 1 3 5 5 2
  2. 2 4 6 2 1
  3. 3 6 9 1 1

The desired output would be:

  1. 1 5 5 3 2
  2. 2 6 2 4 1
  3. 3 9 1 6 1

I know I could loop the following code until I 1 by 1 swap every column until the column is in the desired location.

  1. for (int[] array1 : array) {
  2. int temp = array1[col1];
  3. array1[col1] = array1[col1];
  4. array1[col2] = temp;
  5. }

However, if I'm using large matrices such as 30 columns wide, this would be incredibly inefficient. Is there a way to insert the column anywhere in the 2D array without iterating through each swap until it is in the right spot?

答案1

得分: 2

可能通过使用流进行并行处理来提高性能。

应该使用IntStream的行索引来单独处理每一行。

  1. // 基本的位移方法
  2. // from, to - 从1开始的索引
  3. public static void shiftArray(int[] arr, int from, int to) {
  4. int tmp = arr[from - 1];
  5. for (int i = from; i < to; i++) {
  6. arr[i - 1] = arr[i];
  7. }
  8. arr[to - 1] = tmp;
  9. }
  10. public static void main(String[] args) {
  11. int[][] arr2d = {
  12. {1, 3, 5, 5, 2},
  13. {2, 4, 6, 2, 1},
  14. {3, 6, 9, 1, 1}
  15. };
  16. int fromColumn = 2;
  17. int toColumn = 4;
  18. IntStream.range(0, arr2d.length)
  19. .parallel()
  20. .forEach(i -> shiftArray(arr2d[i], fromColumn, toColumn)); // 并行处理每一行
  21. // 输出位移后的二维数组
  22. Arrays.stream(arr2d)
  23. .map(Arrays::toString)
  24. .forEach(System.out::println);
  25. }

输出:

  1. [1, 5, 5, 3, 2]
  2. [2, 6, 2, 4, 1]
  3. [3, 9, 1, 6, 1]
英文:

Possibly, a performance may be improved by using parallel processing with streams.

IntStream of row indexes should be used to handle each row separately.

  1. // basic shift method
  2. // from, to - indexes starting from 1
  3. public static void shiftArray(int[] arr, int from, int to) {
  4. int tmp = arr[from - 1];
  5. for (int i = from; i &lt; to; i++) {
  6. arr[i - 1] = arr[i];
  7. }
  8. arr[to - 1] = tmp;
  9. }
  10. public static void main(String[] args) {
  11. int[][] arr2d = {
  12. {1, 3, 5, 5, 2},
  13. {2, 4, 6, 2, 1},
  14. {3, 6, 9, 1, 1}
  15. };
  16. int fromColumn = 2;
  17. int toColumn = 4;
  18. IntStream.range(0, arr2d.length)
  19. .parallel()
  20. .forEach(i -&gt; shiftArray(arr2d[i], fromColumn, toColumn)); // shift each row in parallel
  21. // print the 2D array after shift
  22. Arrays.stream(arr2d)
  23. .map(Arrays::toString)
  24. .forEach(System.out::println);
  25. }

Output:

  1. [1, 5, 5, 3, 2]
  2. [2, 6, 2, 4, 1]
  3. [3, 9, 1, 6, 1]

答案2

得分: 1

尝试这个。

  1. public static void moveColumn(int[][] matrix, int from, int to) {
  2. --from; --to; // 如果列号从零开始,请删除此行。
  3. int srcPos = from < to ? from + 1 : to;
  4. int destPos = from < to ? from : to + 1;
  5. int length = Math.abs(from - to);
  6. for (int[] array : matrix) {
  7. int temp = array[from];
  8. System.arraycopy(array, srcPos, array, destPos, length);
  9. array[to] = temp;
  10. }
  11. }

  1. int[][] matrix = {
  2. {1, 3, 5, 5, 2},
  3. {2, 4, 6, 2, 1},
  4. {3, 6, 9, 1, 1}
  5. };
  6. moveColumn(matrix, 2, 4);
  7. for (int[] row : matrix)
  8. System.out.println(Arrays.toString(row));

输出

  1. [1, 5, 5, 3, 2]
  2. [2, 6, 2, 4, 1]
  3. [3, 9, 1, 6, 1]
英文:

Try this.

  1. public static void moveColumn(int[][] matrix, int from, int to) {
  2. --from; --to; // If column number begins with zero, remove this line.
  3. int srcPos = from &lt; to ? from + 1 : to;
  4. int destPos = from &lt; to ? from : to + 1;
  5. int length = Math.abs(from - to);
  6. for (int[] array : matrix) {
  7. int temp = array[from];
  8. System.arraycopy(array, srcPos, array, destPos, length);
  9. array[to] = temp;
  10. }
  11. }

and

  1. int[][] matrix = {
  2. {1, 3, 5, 5, 2},
  3. {2, 4, 6, 2, 1},
  4. {3, 6, 9, 1, 1}
  5. };
  6. moveColumn(matrix, 2, 4);
  7. for (int[] row : matrix)
  8. System.out.println(Arrays.toString(row));

output

  1. [1, 5, 5, 3, 2]
  2. [2, 6, 2, 4, 1]
  3. [3, 9, 1, 6, 1]

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

发表评论

匿名网友

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

确定