Java中最常用的方法是验证转换long为int不会丢失任何信息?
这是我目前的实施:
public static int safeLongToInt(long l) {
int i = (int)l;
if ((long)i != l) {
throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");
}
return i;
}
Run Code Online (Sandbox Code Playgroud) 我尝试了几种不成功的方法.
this.tileUpdateTimes是long[]和other.tileUpdateTimes是int[]
this.tileUpdateTimes = Arrays.stream(other.tileUpdateTimes).toArray(size -> new long[size]);
this.tileUpdateTimes = Arrays.stream(other.tileUpdateTimes)
.map(item -> ((long) item)).toArray();
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
Long x;
int y = (int) x;
Run Code Online (Sandbox Code Playgroud)
Eclipse正在使用错误标记此行:
无法将Long强制转换为int