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数组 }
另外补充两个细节:
- 你的
transpose方法仅适用于方阵(行数=列数),如果后续要处理非方阵需要修改逻辑。 - 如果要验证排序效果,记得在打印前调用对应的排序方法(比如
sortByRow或sortMatRowAndColWise)。
内容的提问来源于stack exchange,提问作者Kaiki
相关产品推荐
相关产品推荐

