首页 > 解决方案 > 如何返回未使用的空间数组?

问题描述

这是指令:返回一个 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;
   }

标签: java

解决方案


public int[] counts()int[]返回类型。该方法希望您返回一个一维数组。我从您的问题和代码中了解到的是,当二维数组中的空格为 时0,您需要将计数器加一,但您并没有增加计数器,您只是将其设置为零。你会想要改变,

count = 0;

count++; //it's the same as count += 1 or count = count + 1

并将其作为一维数组返回:

return new int[] {count};

推荐阅读