Unloq Frit :
I'm having some issues trying to remove all 0 values from an array in Java, the array is pre-defined with a maximum number of values being 50. If the user inputs 10 values then there are always 40 x 0's remaining in the table.
I am attempting to find the length of this array not including 0 values, so the length of the array should only be defined by values 1 and up.
Here is a snippet of my code.
double[] storedValues = {32.0,42.4,34.5,32.4,0,0,0,0,0,0,0,0,0,0...}
arrayLength = storedValues.length;
Currently, arrayLength will return "50" as that is the pre-defined size of my array. I am trying to make this return "4" as there are only 4 values over 0 in this array.
Any help is much appreciated.
Elliott Frisch :
Stream storedValues
, filter for elements greater than 0
and then count. Like,
int count = (int) Arrays.stream(storedValues).filter(d -> d > 0).count();
Or, loop and count yourself
int count = 0;
for (double d : storedValues) {
if (d > 0) {
count++;
}
}