You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中矩阵行式排序遇int转int[][]类型不兼容错误求助

解决矩阵行排序时的类型不兼容错误

你遇到的incompatible types: int cannot be converted to int[][]错误,根源是调用printMat方法时传错了参数:

  • printMat方法的第一个参数要求是int[][]类型的二维数组,但你在main方法里传的是int类型的变量m(行数),正确的参数应该是你创建的二维数组array。

修正后的main方法代码如下:

public static void main(String args[]){
    int m,n,i,j;
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the number of rows: ");
    m = sc.nextInt();
    n = m;
    
    int array[][] = new int[m][n];
    System.out.println("Enter the elements of the array: ");
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
            array[i][j] = sc.nextInt();
    
    // 如果需要执行行排序,取消下面这行注释
    // sortByRow(array, n);
    // 如果需要执行行列排序,取消下面这行注释
    // sortMatRowAndColWise(array, n);
    
    System.out.println("Elements of the array are: ");
    printMat(array, n); // 这里修正为传入array数组
}

另外补充两个细节:

  1. 你的transpose方法仅适用于方阵(行数=列数),如果后续要处理非方阵需要修改逻辑。
  2. 如果要验证排序效果,记得在打印前调用对应的排序方法(比如sortByRow或sortMatRowAndColWise)。

内容的提问来源于stack exchange,提问作者Kaiki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 18:24:27