如何使用java处理矩阵

如何使用java处理矩阵,java,matrix,multidimensional-array,Java,Matrix,Multidimensional Array,我已经想出了一个代码来计算矩阵的转置,但它似乎给出了一个错误。。。任何关于改进代码的建议都会有所帮助 public class Prob1_Matrices { public static int[][] Transpose2D(int m[][]) { int B[][] = new int[m.length][m[0].length]; for (int i = 0; i < m.length; i++) { for (

我已经想出了一个代码来计算矩阵的转置,但它似乎给出了一个错误。。。任何关于改进代码的建议都会有所帮助

public class Prob1_Matrices {

    public static int[][] Transpose2D(int m[][]) {
       int B[][] = new int[m.length][m[0].length];

        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[0].length; j++) {
                int temp = B[i][j];
                B[i][j] = B[j][i]; // this is line 10
                B[j][i] = temp;
            }
        }
        return B;
    }

    public static void main(String[] args) {
        int m[][] = {{2, 5, 8, 3, 6},
        {4, 2, 9, 3, 1},
        {8, 0, 9, 5, 2}};
        System.out.println("Matrix:");
        System.out.println(Transpose2D(m)); //this is line 22
    }

}
公共类Prob1\u矩阵{
公共静态int[]]Transpose2D(int m[]]{
int B[][]=新的int[m.length][m[0].length];
对于(int i=0;i
B是转置矩阵,所以它的大小应该与m相反

如m所示,尺寸为:

m.length * m[0].length
B应是:

m[0].length * m.length
    for (int i = 0; i < m[0].length; i++) {
        for (int j = 0; j < m.length; j++) {
            B[i][j] = m[j][i];
        }
    }
a | b | c

d | e | f

应转置到

a | d

b|e

c | f

另一个问题是在函数中不使用m。 正确的使用方法应该是:

m[0].length * m.length
    for (int i = 0; i < m[0].length; i++) {
        for (int j = 0; j < m.length; j++) {
            B[i][j] = m[j][i];
        }
    }
for(int i=0;i

编辑:交换i和j限制。多亏了大卫·华莱士

B是转置矩阵,所以也许它的大小应该与m相反

B[i][j] = B[j][i];
B[j][i] = temp;
如m所示,尺寸为:

m.length * m[0].length
B应是:

m[0].length * m.length
    for (int i = 0; i < m[0].length; i++) {
        for (int j = 0; j < m.length; j++) {
            B[i][j] = m[j][i];
        }
    }
a | b | c

d | e | f

应转置到

a | d

b|e

c | f

另一个问题是在函数中不使用m。 正确的使用方法应该是:

m[0].length * m.length
    for (int i = 0; i < m[0].length; i++) {
        for (int j = 0; j < m.length; j++) {
            B[i][j] = m[j][i];
        }
    }
for(int i=0;i
编辑:交换i和j限制。感谢大卫·华莱士

B[i][j] = B[j][i];
B[j][i] = temp;
如果您的矩阵不是正方形,这可能会引发ArrayIndexOutOfBoundsException(如果您的数组是例如2x10,您尝试
array[1][8]=array[8][1]
),您的意思可能更像:

public static int[][] Transpose2D(int m[][]) {
   int B[][] = new int[m[0].length][m.length];

    for (int i = 0; i < m.length; i++) {
        for (int j = 0; j < m[0].length; j++) {
            B[j][i] = m[i][j];
        }
    }
    return B;
}
publicstatic int[]]Transpose2D(int m[]]{
int B[][]=新int[m[0]。长度][m.length];
对于(int i=0;i
如果您的矩阵不是正方形,这可能会引发ArrayIndexOutOfBoundsException(如果您的数组是例如2x10,您尝试
array[1][8]=array[8][1]
),您的意思可能更像:

public static int[][] Transpose2D(int m[][]) {
   int B[][] = new int[m[0].length][m.length];

    for (int i = 0; i < m.length; i++) {
        for (int j = 0; j < m[0].length; j++) {
            B[j][i] = m[i][j];
        }
    }
    return B;
}
publicstatic int[]]Transpose2D(int m[]]{
int B[][]=新的int[m[0].length][m.length];
对于(int i=0;i
您的代码中有三个问题。

首先,您会得到ArrayIndexOutOfBoundsException,因为这里:

B[i][j]=B[j][i]

您正试图访问数组B中不存在的索引

B的大小应声明为
int B[][]=new int[m[0].length][m.length]正如您希望B矩阵是m的转置。

其次,在计算B数组时,不使用
m
数组。我也不明白为什么要使用临时变量
int-temp
,它应该满足您的需要:

B[j][i]=m[i][j]

第三个问题是你在这里:
System.out.println(Transpose2D(m))
您正在尝试按原样打印阵列。那是行不通的。试试这个:

System.out.println(Arrays.deepToString(Transpose2D(m)))

代码中有三个问题。

首先,您会得到ArrayIndexOutOfBoundsException,因为这里:

B[i][j]=B[j][i]

您正试图访问数组B中不存在的索引

B的大小应声明为
int B[][]=new int[m[0].length][m.length]正如您希望B矩阵是m的转置。

其次,在计算B数组时,不使用
m
数组。我也不明白为什么要使用临时变量
int-temp
,它应该满足您的需要:

B[j][i]=m[i][j]

第三个问题是你在这里:
System.out.println(Transpose2D(m))
您正在尝试按原样打印阵列。那是行不通的。试试这个:

System.out.println(Arrays.deepToString(Transpose2D(m)))

您有三个问题-第一个问题是您为
B
声明了错误的维度。它应该是
newint[m[0].length][m.length]
,而不是相反

第二个问题是,在为
B
创建任何数据之前,您试图将条目从
B
复制回自身。所以你实际上什么都没有复制。第三个问题-即使在
B
中有条目,每对条目也要交换两次,因此此代码永远不会有任何效果


您希望将条目从
m
复制到
B
。所以在循环中,你需要
B[j][i]=m[i][j]
您没有尝试交换
B

中的条目,而是遇到了三个问题-第一个问题是您为
B
声明了错误的维度。它应该是
newint[m[0].length][m.length]
,而不是相反

第二个问题是,在为
B
创建任何数据之前,您试图将条目从
B
复制回自身。所以你实际上什么都没有复制。第三个问题-即使在
B
中有条目,每对条目也要交换两次,因此此代码永远不会有任何效果


您希望将条目从
m
复制到
B
。所以在循环中,你需要
B[j][i]=m[i][j]
而不是尝试交换
B

中的条目,它在第10行和第22行显示异常。。。。我在第10行和第22行指定了表示异常的一个。。。。我指定了哪些