java java获取数组值的个数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12135748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java get the number of array values
提问by HymanyBoi
I already have the following code
我已经有了以下代码
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
}
}
I must use this code there is no changing this is a requirment, so please dont suggest to use arraylist and such (i am aware of other array types and methods)
我必须使用这段代码,没有改变这是一个要求,所以请不要建议使用 arraylist 等(我知道其他数组类型和方法)
The problem is that as I have already declared that accnums
must contain only 5 values, that is predefined.
问题是,正如我已经声明的那样,它accnums
必须只包含 5 个值,这是预定义的。
I am trying to perform a check whether the ones that are not null and if all are null. To do this I have tried this but this is giving me 5 p(pre-defined integer array value not what I want).
我正在尝试检查那些不为空的是否为空以及是否全部为空。为此,我已经尝试过,但这给了我 5 p(预定义的整数数组值不是我想要的)。
回答by MadProgrammer
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);
}
回答by Kumar Vivek Mitra
Try this...
试试这个...
accnums[0] = new Integer(1);
accnums[1] = new Integer(2);
Both the below will work if done during Declaration and Initializing time of and array.
如果在数组的声明和初始化时间期间完成,则以下两项都将起作用。
Integer[] arr = new Integer[]{1,2,3};
Integer[] arr = {1,2,3}
But when you just declare the array as
但是当你只是将数组声明为
Integer[] arr = new Integer[3]; // Still array holds no Object Reference Variable
then later initialize it this way...
然后稍后以这种方式初始化它......
arr = new Integer{1,2,3,}; // At this time it hold the ORV
Array are always initialized whether used at class or method scope, so for an int array all the values will be set to default as 0, and for Integer
it will be null
, as its an Wrapper object
.
数组总是被初始化,无论是在类还是方法范围内使用,因此对于 int 数组,所有值都将设置为默认值 0,对于Integer
它,它将是null
,作为它的 an Wrapper object
。
Eg:
例如:
Integer[] arr = new Integer[5];
arr[0] = 1;
arr[1] = 2;
System.out.println(arr.length);
for (Integer i : arr){
if (i!=null){
count++;
}
}
System.out.println("Total Index with Non Null Count :"+count);
}
}
回答by oldrinb
accnums[0] = 1;
accnums[1] = 2;
final int count = accnums.length
- Collections.frequency(Arrays.asList(accnums), null);
System.out.println("You have used " + count + " slots");
or, if you really must do it manually...
或者,如果您真的必须手动进行...
int count;
for (final Integer val : accnums) {
if (val != null) {
++count;
}
}