我已经有以下代码
public class Qn3
{
static BigDecimal[] accbal= new BigDecimal[20];
private static Integer[] accnums = new Integer[5];
public static void main(String[] args)
{
int count;
accnums = {1,2} //i cant add this line of code as well, what is wrong?
while(accnums.length < 5)
{
count = accnums.number_of_filled_up_indexes
//this is not actual code i know
//do this as the number of values in the array are less than 5
break;
}
//do this as number of values in the array are more than 5
}
}
我必须使用此代码,无需更改,这是一项要求,因此请不要建议使用arraylist等(我知道其他数组类型和方法)
问题是,正如我已经声明的那样,
accnums
必须仅包含5个预定义的值。我正在尝试检查是否不为null以及是否均为null。为此,我已经尝试过了,但这给了我5 p(预定义的整数数组值不是我想要的值)。
最佳答案
public static void main(String[] args)
{
int count = 0;
accnums = new Integer[] {1,2,null,null,null};
for (int index = 0; index < accnums.length; index++)
{
if(accnums[index] != null)
{
count++;
}
}
System.out.println("You have used " + count + " slots);
}