![](/img/trans.png)
[英]I want a method that accepts an array, low number, high number. and returns an array with only numbers in between that
[英]make a method which returns the number of one digit numbers in an array
我正在尝试创建一种方法,该方法将查找数组中一位数字的数量并返回该值,例如,如果我的数组是 [1,22,3,4,55,66,5] 它将返回 4,因为有 4 个一位数字并且是在 java 中
因为我很无聊...
public static int getOneDigitNumberCount(final int[] numbers)
{
if (numbers == null || numbers.length == 0)
{
return 0;
}
int count = 0;
for (int number : numbers)
{
// assumes you're not considering negative numbers
if (number >= 0 && number <= 9)
{
count++;
}
}
return count;
}
另一种使用streams
并考虑negative
一位数的解决方案。
int[] nums = {1,22,3,4,-8, -9, -45, 55,66,5};
long count = Arrays.stream(nums)
.filter(n -> n > -10 && n < 10)
.count();
System.out.println(count); // prints 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.