Java 从 long 到 int 的可能有损转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21828099/
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
Possible Lossy conversion from long to int
提问by ranaarjun
I wish to enter one int
and another long
ex: 1 and 1000000000, and now I wish to create an array of size 1000000000. And then at each index of array, store int val, ex: arr[100000000] = 4
.
我希望输入一个int
和另一个long
ex: 1 和 1000000000,现在我希望创建一个大小为 1000000000 的数组。然后在数组的每个索引处,存储 int val, ex: arr[100000000] = 4
。
When I'm trying to do this Netbeans shows me an error in this line:
当我尝试这样做时,Netbeans 在这一行中显示了一个错误:
arr = new long[y+1]` and `arr[j] = 0`
"Possible Lossy conversion from long to int". Here's my code:-
“从 long 到 int 的可能有损转换”。这是我的代码:-
public static void main(String[] args) throws IOException
{
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
String[] xe = s.readLine().split(" ");
int x = Integer.parseInt(xe[0]);
long y = Long.parseLong(xe[1]);
long []arr;
arr = new long[y+1];
for(long j=0;j<=y;j++)
arr[j] = 4;
}
采纳答案by PeterMmm
Array index is an integer in Java and the compiler will advice you. So maximum array size is (aproximately) Integer.MAX_VALUE
. For bigger arrays you should use ArrayList.
数组索引是 Java 中的整数,编译器会建议您。所以最大数组大小是 (aproxiately) Integer.MAX_VALUE
。对于更大的数组,您应该使用 ArrayList。
To go around this dirt&quick
绕过这个污垢&快速
arr = new long[(int)y+1];
回答by Leo Pflug
You need to convert/cast y and j to int. Also your incrementing var shouldn't be long when you're just adding 1.
您需要将 y 和 j 转换/转换为 int。此外,当您仅添加 1 时,您的递增 var 不应太长。
回答by Marco13
The size of an array can only be an int
. That is you, can not create arrays that are larger than Integer.MAX_VALUE (2147483647), probably a few elements less (depending on the VM). You can cast your value to int
数组的大小只能是int
. 那就是你,不能创建大于 Integer.MAX_VALUE (2147483647) 的数组,可能少几个元素(取决于 VM)。你可以把你的价值投给int
arr = new long[(int)(y+1)];
but this will produce invalid results when the value of y
is actually larger than the maximum allowed size.
但是当 的值y
实际上大于允许的最大大小时,这将产生无效的结果。
回答by barak manos
You cannot create an array with more than 2^31-1 entries, so you should use an
int
value when you do so (the compiler is simply warning you that the size will be truncated fromlong
toint
). 1000000000 is small enough to fit intoint
, so you basically have no reason to uselong y
in order to store this value in the first place.According to your description, the array itself is designated to store
int
values, so it doesn't need to be an array oflong
values.
您不能创建包含超过 2^31-1 个条目的数组,因此您应该
int
在这样做时使用一个值(编译器只是警告您大小将从long
to截断int
)。1000000000 小到可以放入 中int
,因此您基本上没有理由使用long y
它来首先存储此值。根据你的描述,数组本身是用来存储
int
值的,所以它不需要是一个long
值数组。
In short, you can and shouldchange every long
in your code to int
.
简而言之,您可以而且应该long
将代码中的每个更改为int
.
回答by AjayLohani
I think you have some misconception about typecasting here. In Java down casting is allowed as already mentioned you can down cast it to integer
我认为你对这里的类型转换有一些误解。如前所述,在 Java 中允许向下转换,您可以将其向下转换为整数
arr = new long[(int)(y+1)];
But here the main problem is that array allocation should takes integer value so that the same number of homogeneous space can be allocated. If you want more space than an integer range then you should use ArrayList which is dynamically grow-able array because if we give that much liability to a user to declare a large amount array memory then our system may run out of the memory as array is a static memory allocation data structure. The same stands true for
但这里的主要问题是数组分配应该采用整数值,以便可以分配相同数量的同构空间。如果你想要比整数范围更多的空间,那么你应该使用 ArrayList,它是动态可增长的数组,因为如果我们给用户很大的责任来声明大量的数组内存,那么我们的系统可能会耗尽内存,因为数组是静态内存分配数据结构。同样适用于
arr[j]=4;
It should be:
它应该是:
arr[(int)j]=4;