java 计算数组中的负数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12674088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compute the negative numbers in an array
提问by PHZE OXIDE
I was searching for solution to find negative numbers in array and i came up with something like this code from searching.
我正在寻找在数组中查找负数的解决方案,我从搜索中想出了类似这样的代码。
public static void main(String args[]){
int arrayNumbers[] = { 3, 4, 7, -3, -2};
for (int i = 0; i <= arrayNumbers.length; i++){
int negativeCount = 0;
if (arrayNumbers[i] >= 0){
negativeCount++;
}
System.out.println(negativeCount);
}
}
I was wondering is there an easier or shorter way to find the negative number in an array vs the code above?
我想知道是否有更简单或更短的方法来查找数组中的负数与上面的代码?
回答by Bohemian
A java 7 string-based one-liner that counts the minus signs:
一个 java 7 基于字符串的单行,计算减号:
System.out.println(Arrays.toString(array).replaceAll("[^-]+", "").length());
A Java 8 stream-based way:
Java 8 基于流的方式:
System.out.println(Arrays.stream(array).filter(i -> i < 0).count());
Regarding your code, there are a few things wrong with it:
关于您的代码,它有一些问题:
- Since you don't care about the index of an element, use the foreach syntaxinstead
- Declare the scope of your count variable outsidethe loop, otherwise
- it keeps getting set to zero every iteration, and
- you couldn't use it even if it did contain the correct count because it would be out of scope (which would be only inside the loop) where you need to return it (after the loop)
- Use the correct test
number < 0
(your code>= 0
counts nonnegative numbers)
- 既然你不关心元素的索引,使用的foreach的语法,而不是
- 在循环外声明 count 变量的范围,否则
- 每次迭代它都会被设置为零,并且
- 即使它确实包含正确的计数,您也无法使用它,因为它将超出范围(仅在循环内)您需要返回它(在循环之后)
- 使用正确的测试
number < 0
(您的代码>= 0
计算非负数)
Try this:
试试这个:
public static void main(String args[]) {
int[] array = { 3, 4, 7, -3, -2};
int negativeCount = 0;
for (int number : array) {
if (number < 0) {
negativeCount++;
}
}
System.out.println(negativeCount);
}
回答by hmjd
A few issues with the code:
代码的几个问题:
- the terminating condition in the
for
will produce an out of bounds exception (arrays use zero-based index) - the scope of
negativeCount
is within thefor
only - the negative check is incorrect
- 中的终止条件
for
将产生越界异常(数组使用从零开始的索引) - 范围
negativeCount
是for
唯一的 - 否定检查不正确
A slightly shorter version would use the extended for
:
稍短的版本将使用扩展的for
:
int negativeCount = 0;
for (int i: arrayNumbers)
{
if (i < 0) negativeCount++;
}
For a shorter version (but arguably less readable) eliminate the for
's {}
:
对于较短的版本(但可以说可读性较差),请消除for
's {}
:
int negativeCount = 0;
for (int i: arrayNumbers) if (i < 0) negativeCount++;
回答by Rohit Jain
Your negativeCount should be declared outside your loop.. Also, you can move your System.out.println(negativeCount)
outside your loop, as it will print for every iteration..
你的negativeCount应该在你的循环之外声明..此外,你可以将你System.out.println(negativeCount)
的循环移到你的循环之外,因为它会在每次迭代时打印..
And you can use enhanced-forloop
你可以使用增强的for循环
public static void main(String args[]){
int arrayNumbers[] = { 3, 4, 7, -3, -2};
int negativeCount = 0;
for (int num: arrayNumbers) {
if (num < 0){
negativeCount++;
}
}
System.out.println(negativeCount);
}
回答by josefx
A bit shorter with the foreach syntax:
使用 foreach 语法稍微短一点:
int negativeCount = 0;
for(int i : arrayNumbers)
{
if(i < 0)negativeCount++;
}