java - 如何返回未使用的空间数组?
问题描述
这是指令:返回一个 int 数组,其中 array[0] 是未使用空格的数量这是我到目前为止的代码,但我不确定我是否做得对(或者我需要在方法中返回什么)
public int[] counts()
{
int count=0;
for(int i=0; i<array.length;i++)
{
for (int j=0; j<array.length; j++)
{
if (array[i][j] == 0)
{
count = 0;
}
}
}
return;
}
解决方案
public int[] counts()
是int[]
返回类型。该方法希望您返回一个一维数组。我从您的问题和代码中了解到的是,当二维数组中的空格为 时0
,您需要将计数器加一,但您并没有增加计数器,您只是将其设置为零。你会想要改变,
count = 0;
至
count++; //it's the same as count += 1 or count = count + 1
并将其作为一维数组返回:
return new int[] {count};
推荐阅读
- gis - 用于开发 gnome shell 扩展的文档
- pdf - 使用 gs 时如何修复“gsicc_open_search():找不到”错误消息
- java - java jtable中用户登录后如何显示mysql数据库中的特定用户数据;
- sql - 用于存储字符串值的 VBA(AUTOCAD) SQL 查询
- javascript - 如何使用 underscoreJS 过滤对象数组?
- python - 使用函数在 df 中添加列
- spring - 如何区分提供 404 的 Apache Tomcat 和我在 tomcat 上返回 404 的应用程序?
- r - 如何在 R 中转换超过 3 列的数据框?
- java - 即使用 VBA 编写,如何使用按钮单击执行 Java 程序
- spring-boot - /env/reset 和 /refresh 之间的区别