gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:58:17 26 4
gpt4 key购买 nike

我给了谷歌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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com