Java: How can I get the size of Integer(object) array? -


I'm talking about the integer object data type, not primitive int

Suppose:

  integer [] arr = new integer [5]; Arr [0] = 2; Arr [1] = 2; Advent [2] = 3;  

Then the array will look like this {2,2,3, tap, blank}

how can I get the shape of Array? In this case, size is 5

Similar to any other array - you type length < / Code> field:

  int length = arr.length; // 5  

to:

The members of an array type are all of the following:

  • Public Last Region Length , which contains the number of components of the array, length may be positive or zero.
  • ...
Blockquote>

If you want to count non-zero values, then you will use something like this:

< Pre> integer number = 0; For (integer value: arr) {if (value! = Null) {count ++; }}

(I'm sure there is a clever way to do this in lower lines using the Streams API in Java 8, but we keep it simple so far ...)


Comments

Popular posts from this blog

HTML/CSS - Automatically set height width from background image? -

php - Mysql Show Process - Sleep Commands and what to do -

c - What is the address of buf (the local variable in the main function)? -