java - Java,以初始值设定项列表格式将2d数组传递给方法

标签 java google-foobar

我给了谷歌foobar,并且我对Java语言没有太多的经验。请在此示例代码中帮助我,以将2D数组传递给静态公共方法。它说,它使用标准的Java 8。

class Ideone
{
    public static void solution(int [][]a){
        System.out.println(a.length);
    }
    public static void main (String[] args) throws java.lang.Exception
    {

        int [][]arr = {{1,3}, {4,5}}; 
        solution(arr); // This was executed successfully
        // But when I tried this
        solution({{1,3}, {4,5}}); 
        // This error displayed : 
        //Main.java:18: error: illegal start of expression solution({{1,3}, {4,5}});
        //                                                            ^
    }
}


我需要以这种格式传递它,因为Google foobar的方法声明为

public static int solution(int[][] grid){
}
// Input was given like below,  in the readme file

Solution.solution({{0, 1, 1, 0}, {0, 0, 0, 1}, {1, 1, 0, 0}, {1, 1, 1, 0}})


请帮帮我,我很少有时间来完成它。

最佳答案

为了正确地初始化数组,请执行以下操作:
solution(new int [] [] {{1,3},{4,5}});

关于java - Java,以初始值设定项列表格式将2d数组传递给方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61554666/

相关文章:

java - 卡塔西斯压制关系链接?

java - Druid 中的 Parquet 数据摄取使用 Joda 进行时间戳解析时出错

java - 尝试获取响应代码时代码挂起

java - 如何打开 AWT 日志记录

Java 从一个 ArrayList 生成两个 ArrayList