我想在自定义矩阵类中交换矩阵对角线中的项目

时间:2016-12-13 19:25:44

标签: java matrix

我想创建一个方法,可以在我的自定义矩阵类中交换矩阵对角线中的项目。

如果这是输入:

1,0,0,5
0,2,6,0
0,7,3,0
8,0,0,4

这将是输出:

4,0,0,8
0,3,7,0
0,6,2,0
5,0,0,1

这是我的代码:

import java.util.*;
public class Matrix
{
public final static int MAX_INTEGER = 50;
    public final static int MAX_SIZE = 20;

    private int[][] matrix;


    public Matrix(int[][] matrix)
    {
        if (matrix.length >= 0 && matrix.length <= MAX_SIZE)
            this.matrix = matrix;
        else if (matrix.length < 0)
            throw new RuntimeException("The length of the array can not be negative");
        else
            throw new RuntimeException("The length of the array can not be greater than 20");

        Random generator = new Random();

        for (int i=0; i < matrix.length; i++)
            for (int j=0; j < matrix.length; j++)
                matrix[i][j] = generator.nextInt(MAX_INTEGER)+1;
    }


    public void swapDiagonals()
    {   
        int aux=0;


        for (int i=0; i < matrix.length; i++)
            for (int j=0; j < matrix.length; j++)
                if(i == j)
                {                 
                    aux = matrix[i][j];
                    matrix[i][j] = matrix[matrix.length-1-i][matrix.length-1-j];
                    matrix[matrix.length-1-i][matrix.length-1-j] = aux;
                }

        for (int i=0; i < matrix.length; i++)
            for (int j=0; j < matrix.length; j++)
                if(i == matrix.length-j)
                {   
                    aux = matrix[i][matrix.length-1-j];
                    matrix[i][matrix.length-1-j] = matrix[matrix.length-1-i][j];
                    matrix[matrix.length-1-i][j] = aux;
                }    


    }

问题是该方法没有做任何事情。如果我在运行方法后检查矩阵,则没有任何改变。

这应该是一个成功的结果:

    @Test
    public void testSwapDiagonals()
    {
        Matrix dummy = new Matrix(new int[4][4]);
        assertEquals(int[][]={{13,15,23,6},{17,37,15,6},{10,7,48,45},{4,47,41,21}}, dummy.getMatrix());
        dummy.swapDiagonals();
        assertEquals(int[][]={{21,15,23,4},{17,48,7,6},{10,15,37,45},{6,47,41,13}}, dummy.getMatrix());
    }

抱歉,我应该更具体一点:我不想交换所有对角线。我想要this

1 个答案:

答案 0 :(得分:0)

此代码应该可以工作,交换方形矩阵的对角线

public void swapMatrixDiagonals() {

    int up = 0; //Can be replaced by variable 'i' in loop
    int down = matrix.length - 1;
    int left = 0; //Can be replaced by variable 'i' in loop
    int right = matrix[matrix.length - 1].length - 1;

    for(int i = 0; i < matrix.length / 2; i++) {
        int tempLeft = matrix[up][left];
        int tempRight = matrix[up][right];

        matrix[up][left] = matrix[down][right];
        matrix[down][right] = tempLeft;

        matrix[up][right] = matrix[down][left];
        matrix[down][left] = tempRight;

        left++;
        right--;
        up++;
        down--;
    }
}

变量“up”和“left”在这里只是为了更好地可视化代码正在做什么,可以通过在循环中递增“i”来替换

请注意,它可以生成“ArrayIndexOutOfBoundsException” 如果矩阵不是方形的 e.g。

A B C D
E - - -
F G H I
J K L M

如果您想要交换所有位置,则需要更多循环,这里是这种操作的示例代码

public void swap() {
    int up = 0; //Can be replaced with variable 'i' in loop
    int down = matrix.length - 1;
    for(int i = 0; i < matrix.length; i++) {
        int left = 0; //Can be replaced with variable 'j' in loop
        int right = matrix[i].length - 1;
        for(int j = 0; j < matrix[i].length / 2; j++) {
            int temp = matrix[i][left];
            matrix[i][left] = matrix[i][right];
            matrix[i][right] = temp;
            left++;
            right--;
        }
    }
    for(int i = 0; i < matrix.length / 2; i++) {
        int[] temp = matrix[up];
        matrix[up] = matrix[down];
        matrix[down] = temp;
        up++;
        down--;
    }
}