英文:
Insert column in desired location of 2D array
问题
我想选择数组中的一列,比如第2列。我希望将这一列插入到二维数组的特定位置,比如第4列。
例如:
1 3 5 5 2
2 4 6 2 1
3 6 9 1 1
期望的输出将是:
1 5 5 3 2
2 6 2 4 1
3 9 1 6 1
我知道我可以使用以下代码循环,直到逐个交换每一列,直到列在所需位置为止:
for (int[] array1 : array) {
    int temp = array1[col1];
    array1[col1] = array1[col2];
    array1[col2] = temp;
}
然而,如果我使用宽度为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 3 5 5 2
2 4 6 2 1
3 6 9 1 1
The desired output would be:
1 5 5 3 2
2 6 2 4 1
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.
for (int[] array1 : array) {
    int temp = array1[col1];
    array1[col1] = array1[col1];
    array1[col2] = temp;
}
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的行索引来单独处理每一行。
    // 基本的位移方法
    // from, to - 从1开始的索引
    public static void shiftArray(int[] arr, int from, int to) {
        int tmp = arr[from - 1];
        for (int i = from; i < to; i++) {
            arr[i - 1] = arr[i];
        }
        arr[to - 1] = tmp;
    }
    public static void main(String[] args) {
		int[][] arr2d = {
		    {1, 3, 5, 5, 2},
            {2, 4, 6, 2, 1},
            {3, 6, 9, 1, 1}
		};
		int fromColumn = 2;
		int toColumn = 4;
        IntStream.range(0, arr2d.length)
		      .parallel()
		      .forEach(i -> shiftArray(arr2d[i], fromColumn, toColumn)); // 并行处理每一行
        // 输出位移后的二维数组
        Arrays.stream(arr2d)
		      .map(Arrays::toString)
		      .forEach(System.out::println);
    }
输出:
[1, 5, 5, 3, 2]
[2, 6, 2, 4, 1]
[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.
    // basic shift method
    // from, to - indexes starting from 1
    public static void shiftArray(int[] arr, int from, int to) {
        int tmp = arr[from - 1];
        for (int i = from; i < to; i++) {
            arr[i - 1] = arr[i];
        }
        arr[to - 1] = tmp;
    }
    public static void main(String[] args) {
		int[][] arr2d = {
		    {1, 3, 5, 5, 2},
            {2, 4, 6, 2, 1},
            {3, 6, 9, 1, 1}
		};
		int fromColumn = 2;
		int toColumn = 4;
        IntStream.range(0, arr2d.length)
		      .parallel()
		      .forEach(i -> shiftArray(arr2d[i], fromColumn, toColumn)); // shift each row in parallel
        // print the 2D array after shift
        Arrays.stream(arr2d)
		      .map(Arrays::toString)
		      .forEach(System.out::println);
    }
Output:
[1, 5, 5, 3, 2]
[2, 6, 2, 4, 1]
[3, 9, 1, 6, 1]
答案2
得分: 1
尝试这个。
public static void moveColumn(int[][] matrix, int from, int to) {
    --from; --to;  // 如果列号从零开始,请删除此行。
    int srcPos = from < to ? from + 1 : to;
    int destPos = from < to ? from : to + 1;
    int length = Math.abs(from - to);
    for (int[] array : matrix) {
        int temp = array[from];
        System.arraycopy(array, srcPos, array, destPos, length);
        array[to] = temp;
    }
}
和
int[][] matrix = {
    {1, 3, 5, 5, 2},
    {2, 4, 6, 2, 1},
    {3, 6, 9, 1, 1}
};
moveColumn(matrix, 2, 4);
for (int[] row : matrix)
    System.out.println(Arrays.toString(row));
输出
[1, 5, 5, 3, 2]
[2, 6, 2, 4, 1]
[3, 9, 1, 6, 1]
英文:
Try this.
public static void moveColumn(int[][] matrix, int from, int to) {
    --from; --to;  // If column number begins with zero, remove this line.
    int srcPos = from < to ? from + 1 : to;
    int destPos = from < to ? from : to + 1;
    int length = Math.abs(from - to);
    for (int[] array : matrix) {
        int temp = array[from];
        System.arraycopy(array, srcPos, array, destPos, length);
        array[to] = temp;
    }
}
and
int[][] matrix = {
    {1, 3, 5, 5, 2},
    {2, 4, 6, 2, 1},
    {3, 6, 9, 1, 1}
};
moveColumn(matrix, 2, 4);
for (int[] row : matrix)
    System.out.println(Arrays.toString(row));
output
[1, 5, 5, 3, 2]
[2, 6, 2, 4, 1]
[3, 9, 1, 6, 1]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论