简体   繁体   中英

How to get the count of values in a primitive array in Java?

How to find the count of values in a primitive array in Java (not the length)?

int[] array = new int[26];

array[0] = 1;
array[1] = 2;
array[2] = 3;

for (int i=0; i<array.length; i++)
    if (array[i] % 2 != 0)
        return false;

I want the count of the values (3), not the array length (26).

Is there any built-in function available to get this in Java?

    int[] array = new int[26];

    array[0] = 1;
    array[1] = 2;
    array[2] = 3;

    for (int i = 0; i < array[i]; i++)
            System.out.println(i);

remove length, if you add length() in the loop it will return the whole size of an array, instead, count the index of the element which will give the current size of an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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