java.lang.ArithmeticException: / by zero

后端 未结 1 489
温柔的废话
温柔的废话 2020-12-20 07:22

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?

1条回答
  •  一个人的身影
    2020-12-20 08:01

    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.

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

    0 讨论(0)
提交回复
热议问题