具有二维数组的Java程序

具有二维数组的Java程序,java,arrays,Java,Arrays,我正在写一个程序,决定一个数字X是否在输入矩阵nxn中,该数字矩阵已经存储在2D数组的内存中,并且每个单独的行从左到右递增;柱从上到下。我输入矩阵n的大小、内容和要搜索的数字X;我还将在屏幕上显示搜索结果。在测试我的程序时,输入按2D数组排序 这是我的代码,它显示了下面的编译错误 import java.util.*; public class matrix { public static void main (String[] args){ int search(i

我正在写一个程序,决定一个数字X是否在输入矩阵nxn中,该数字矩阵已经存储在2D数组的内存中,并且每个单独的行从左到右递增;柱从上到下。我输入矩阵n的大小、内容和要搜索的数字X;我还将在屏幕上显示搜索结果。在测试我的程序时,输入按2D数组排序

这是我的代码,它显示了下面的编译错误

import java.util.*;

public class matrix
{
    public static void main (String[] args){

        int search(int mat[4][4]; int n; int x)
        {
            int i = 0, j = n-1;  //set indexes for top right element
            while ( i < n && j >= 0 )
            {
                if ( mat[i][j] == x )
                {
                    printf("\n Found at %d, %d", i, j);
                    return 1;
                }
                if ( mat[i][j] > x )
                    j--;
                else //  if mat[i][j] < x
                    i++;
            }

            printf("\n Element not found");
            return 0;  // if ( i==n || j== -1 )
        }

        // driver program to test above function
        int main()
        {
            int mat[4][4] = { {10, 20, 30, 40},
                              {15, 25, 35, 45},
                              {27, 29, 37, 48},
                              {32, 33, 39, 50},
                             };
            search(mat, 4, 29);
            return 0;
        }
    }
}

据我所知,Java不支持本地函数,因此编译器被丢弃。当然,使用“;”分离函数参数也没有帮助

public static void main (String[] args){


int search(int mat[4][4]; int n; int x) // Local method declaration.
您在main中有搜索方法,它应该在外部。另外,您正在main方法内声明另一个main。记住,main方法是程序的入口点,应该只有一个:

public static void main(String[] args) { ... }
在Java中,不应该在参数声明中声明数组的长度。此外,使用逗号分隔参数:

public static int search(int[][] mat, int n, int x)
您应该使用System.out.printf,而不仅仅是printf

最后,可以通过多种方式声明/初始化数组:

int mat[][] = {{1,2}, {3,4}};
int mat[][] = new int[3][3];
int mat[][] = new int[3][];
最终的代码应该如下所示:

public static int search(int[][] mat, int n, int x)
{
    int i = 0, j = n - 1; //set indexes for top right element
    while (i < n && j >= 0) {
        if (mat[i][j] == x) {
            System.out.printf("\n Found at %d, %d", i, j);
            return 1;
        }
        if (mat[i][j] > x) {
            j--;
        } else {
            i++;
        }
    }

    System.out.printf("\n Element not found");
    return 0; // if ( i==n || j== -1 )
}

public static void main(String[] args)
{
    int mat[][] = { { 10, 20, 30, 40 }, { 15, 25, 35, 45 }, { 27, 29, 37, 48 }, { 32, 33, 39, 50 }, };
    search(mat, 4, 29);
}

函数参数由分号而不是逗号分隔。
public static int search(int[][] mat, int n, int x)
{
    int i = 0, j = n - 1; //set indexes for top right element
    while (i < n && j >= 0) {
        if (mat[i][j] == x) {
            System.out.printf("\n Found at %d, %d", i, j);
            return 1;
        }
        if (mat[i][j] > x) {
            j--;
        } else {
            i++;
        }
    }

    System.out.printf("\n Element not found");
    return 0; // if ( i==n || j== -1 )
}

public static void main(String[] args)
{
    int mat[][] = { { 10, 20, 30, 40 }, { 15, 25, 35, 45 }, { 27, 29, 37, 48 }, { 32, 33, 39, 50 }, };
    search(mat, 4, 29);
}