简体   繁体   English

java获取数组值的数量

[英]java get the number of array values

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. 我正在尝试检查那些非null并且是否都为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(预定义的整数数组值不是我想要的)。

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);

}

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 ,因为它是一个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);

} }

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;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM