的下列值低于在Java int的范围错误
long Count = 2151685171
int CurrentPosition = 849
int employeesLeft = 1276
代码引发indexOutOfBound错误:
int EmployeeToBeEliminated = (int)(count+currentPosition-1)%employeesLeft;
而如果这样写的
count = (count+currentPosition-1)%employeesLeft;
int EmployeeToBeEliminated = (int)count;
没有错误抛出。但在第一种情况下,取模后的数值减小到0
至1276
的范围,所以应该很容易铸造成int
。那么为什么它会抛出一个错误?
检查整数。MAX_INT和自动装箱:在第一种情况下,所有值都会转换为int,而在第二种情况下,所有值都会转换为long。 – terjekid 2015-02-09 08:22:16
您包含的代码不会抛出IndexOutOfBoundsException。你可能有一些代码,你使用'EmployeeToBeEliminated'作为一些Collection /数组的索引。 – Eran 2015-02-09 08:22:35
你的变量在你的代码中被称为'Count',但是你使用'count'。 'currentPosition'相同。 – emlai 2015-02-09 08:22:35