java.lang.ArithmeticException: / 为零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26226863/
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
java.lang.ArithmeticException: / by zero
提问by Adway Dhillon
I'm getting this error in this line of code:
我在这行代码中收到此错误:
for (int i = Math.abs(key.hashCode()) % size; i < size; i++)
why is this happening?
为什么会这样?
采纳答案by Jason
The % operator returns the remainder after dividing the first number by the second number. If the second number (in your example size
) is zero, then you will get a divide by zero ArithmeticException
.
% 运算符返回第一个数字除以第二个数字后的余数。如果第二个数字(在您的示例中size
)为零,那么您将得到除以零ArithmeticException
。
The key is to check if size
is zero before performing this loop, and take the appropriate action.
关键是size
在执行此循环之前检查是否为零,并采取适当的措施。