调用数组并返回不同数组的方法是无法将B解析为变量错误

0 java arrays sorting compiler-errors

我正在调用一个数组并返回另一个基于另一个数组排序的数组.

我认为我的方法是正确的,但是当我尝试从主方法中的新数组打印出来时,我收到的错误是" Cannot resolve B to a variable."

B是我的方法应该返回的新数组的名称.
提前致谢.

package sort;

public class SortByFinal {

    public static int [][] sortByFinal(int n, int [][] A)
    {
        int [][]B = new int [n][3];

        int max = 0;

        for(int i =0; i<n; i++)
        {
            for(int j = i; j<n; j++)
            {
                max = Math.max(A[i][1], max);
            }

            for(int k = i; k<n; k++)
            {
                if(A[k][1] == max)
                {
                    B[k][0] = A[k][0];
                    B[k][1] = A[k][1];
                    B[k][2] = A[k][2];
                }
            }
        }

        return B;   

    }

    public static void main(String []args)
    {
        int num = 5;

        int[][] now = new int [num][3];

        now[0][0] = 2342;

        now[0][1] = 88;

        now[0][2] = 98;

        now[1][0] = 3901;

        now[1][1] = 75;

        now[1][2] = 71;

        now[2][0] = 1444;

        now[2][1] = 60;

        now[2][2] = 85;

        now[3][0] = 5327;

        now[3][1] = 95;

        now[3][2] = 80;

        now[4][0] = 4888;

        now[4][1] = 83;

        now[4][2] = 100;

        //int [][] B = new int[num][3];

        sortByFinal(num, now);

        for(int i = 0; i<num; i++)
        {
            for(int j = 0; j<3; j++)
            {
                System.out.print(B[i][j]);//this gives me the "B cannot be resolved to a variable."
                System.out.print(" ");
            }
            System.out.println();
        }



    }

}
Run Code Online (Sandbox Code Playgroud)

Ste*_* P. 5

B是在sortByFinal(),但不是在main().要解决此问题,您可以将主要内容更改为以下内容:

int[][] B = sortByFinal(num, now);
Run Code Online (Sandbox Code Playgroud)

由于sortByFinal(num, now)返回a int[][],这将起作用.

在你的代码中,你正在调用函数,但没有使用返回值做任何事情,并且你无法访问其他方法的局部变量; 因此,消息.