更新时间:2023-11-29 17:28:10
在基本数组元素不能为空。他们将永远得到初始化的东西。
Elements in primitive arrays can't be empty. They'll always get initialized to something
如果您声明数组像这样
int [] newData = new int [17];
,那么所有的元素将默认为零。
then all of the elements will default to zero.
有关检查,如果没有输入的元素,你可以使用一个简单的循环:
For checking if an element is not entered, you can use a simple loop :
for(int i=0;i<newData.length;i++)
{
if(newData[i]==0)
System.out.println("The value at " + i + "is empty");
}
虽然,上述code不会工作你的情况,因为用户可能输入0作为输入值,仍然这code会认为它是空的。
Although , the above code will not work in your case, because the user might enter 0 as an input value and still this code will consider it to be empty.
你可以做的是,初始化所有值的数组为-1,并且在只值> = 0可输入输入提示指定。
初始化可以这样做:
What you can do is, initialize the array with all values as -1, and specify at the input prompt that only values >=0 can be entered . The initialization can be done like this:
int[] newData = new int[17];
for(int i=0;i<newData.length;i++)
{
newData[i]= -1;
}
然后,你可以要求用户输入并做处理。那么你可以使用这样的:
Then you can ask the user for input and do the processing. Then you can use this:
for(int i=0;i<newData.length;i++)
{
if(newData[i]==-1)
System.out.println("The value at " + i + "is empty");
}