交换矩阵的两列

时间:2019-02-09 15:28:16

标签: java

我给出了一个二维数组(矩阵)和两个数字:i和j。我的目标是在矩阵内用索引i和j交换列。输入包含矩阵尺寸n和m,不超过100,然后是矩阵元素,然后是索引i和j。

我想问题的根源与引用变量有关?我尝试将第15行替换为

int nextValue = scanner.nextInt();
matrix[i][j] = nextValue;
swap[i][j] = nextValue;

但输出仍保持不变...

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int row = scanner.nextInt();
        int column = scanner.nextInt();

        int[][] matrix = new int[row][column];
        int[][] swap = matrix.clone();

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        int c0 = scanner.nextInt();
        int c1 = scanner.nextInt();

        for (int i = 0; i < row; i++) {
            swap[i][c0] = matrix[i][c1];
            swap[i][c1] = matrix[i][c0];
        }

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                System.out.print(swap[i][j] + " ");
            }
            System.out.println();
        }

    }
}

我的输入

3 4 
11 12 13 14 
21 22 23 24 
31 32 33 34 
0 1

3和4代表矩阵的行数和列数,以下三行定义矩阵的元素,最后一行告诉程序交换了哪些列。

预期输出:

12 11 13 14 
22 21 23 24 
32 31 33 34 

实际输出:

12 12 13 14 
22 22 23 24 
32 32 33 34

3 个答案:

答案 0 :(得分:0)

您的交换逻辑似乎已关闭。如果要交换两个变量,例如ab,那么这是在Java中有效的模式:

int a = 5;
int b = 10;
int temp = a;
a = b;
b = temp;

将此逻辑应用于矩阵列交换,我们可以尝试以下更新的代码:

int c0 = scanner.nextInt();
int c1 = scanner.nextInt();

for (int i=0; i < row; i++) {
    int temp = matrix[i][c0];
    matrix[i][c0] = matrix[i][c1];
    matrix[i][c1] = temp;
}

答案 1 :(得分:0)

非常感谢@Tim Biegeleisen!

此代码对我有用:

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int row = scanner.nextInt();
    int column = scanner.nextInt();

    int [][] matrix = new int[row][column];
    int [][] swap = matrix.clone();

    for (int i = 0; i < row; i++) {
      for (int j = 0; j < column; j++) {
        matrix[i][j] = scanner.nextInt();}}

    int c0 = scanner.nextInt();
    int c1 = scanner.nextInt();

    for (int i=0; i < row; i++) {
      int temp = matrix[i][c0];
      matrix[i][c0] = matrix[i][c1];
      matrix[i][c1] = temp;
    }

    for (int i = 0; i < row; i++) {
      for (int j = 0; j < column; j++) {
        System.out.print(swap[i][j]+" "); }
      System.out.println();}

  }
}

答案 2 :(得分:0)

在你的程序中,一个逻辑错误是他们的: 看我的代码

import javax.sound.sampled.Line;
import java.util.Arrays;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // put your code here
        int i = scanner.nextInt();
        int j = scanner.nextInt();
        int[][] matrix = new int[i][j];
        for (int y = 0; y < i; y++) {
            for (int x = 0; x < j; x++) {
                matrix[y][x] = scanner.nextInt();
            }
        }
        int swap1 = scanner.nextInt();
        int swap2 = scanner.nextInt();
        for (int[] arr : matrix) {
            int temp = arr[swap1];
            arr[swap1] = arr[swap2];
            arr[swap2] = temp;
            for (int el : arr) {
                System.out.print(el + " ");
            }
            System.out.println();
        }
    }
}