二维数组冒泡排序修复请

2d array Bubble sort fix pls


所以如果你输入 3x3 数组,它应该是这样的 987 654 321

我想对它进行冒泡排序 123 456 789

但我在端线有问题 谁能帮帮我

当我执行我的代码时,输​​出如下所示:

Please enter number of rows:2
Please enter number of columns: 2
Please enter 4 numbers to sort: 8 4 6 2
Sorted numbers:
[[I@776ec8df [[I@776ec8df [[I@776ec8df [[I@776ec8df

import java.util.Scanner;
public class 2dsort 
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("Please enter number of rows: ");
        var row = sc.nextInt();
        
        System.out.print("Please enter number of columns: ");
        var col = sc.nextInt();
        
        int[][] matrix = new int[row][col];
        
        System.out.print("Please enter " + (row*col) + " numbers to sort: ");
        
        for(int x = 0; x < row; x++)
            for(int y = 0; y < col; y++)
            {
                matrix[x][y] = sc.nextInt();
            }
        
        for(int x = 0; x < matrix.length; x++)
            for (int y = 0; y < matrix[x].length; y++)
                for(int t = 0; t < matrix[x].length - y - 1; y++)
                    if(matrix[x][t] > matrix[x][t+1])
                    {
                        int temp = matrix[x][t];
                        matrix[x][t] = matrix[x][t+1];
                        matrix[x][t + 1] = temp;
                    }
    
        //I think this is my problem but idk how to fix
        System.out.println("Sorted numbers:");
        for(int x = 0; x < matrix.length; x++)
            for (int y = 0; y < matrix[x].length; y++)
            System.out.print(matrix + " ");
        System.out.println();
            
    }
}

您的问题似乎在这里:

System.out.print(matrix + " ");

您没有打印出矩阵中的值。

您可能希望使用 matrix[x][y]

检索特定索引处的值

我 运行 您的代码使用 System.out.print(matrix[x][y] + " ");,虽然代码执行有效并适当地打印了矩阵值,但您的排序算法没有给您想要的结果。我会把它留给你解决,但你的打印问题已经解决了。