java.lang.ArithmeticException:/减零 [英] java.lang.ArithmeticException: / by zero

查看:33
本文介绍了java.lang.ArithmeticException:/减零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下代码行中遇到此错误:

I'm getting this error in this line of code:

for (int i = Math.abs(key.hashCode()) % size; i < size; i++)

为什么会这样?

推荐答案

%运算符在将第一个数字除以第二个数字后返回余数.如果第二个数字(在您的示例中为 size )为零,那么您将得到除以零的 ArithmeticException .

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 是否为零,并采取适当的措施.

The key is to check if size is zero before performing this loop, and take the appropriate action.

这篇关于java.lang.ArithmeticException:/减零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆