gpt4 book ai didi

java - 从 long 到 int 的可能有损转换

转载 作者:行者123 更新时间:2023-12-01 05:01:14 24 4
gpt4 key购买 nike

我想输入一个 int和另一个 long例如:1 和 1000000000,现在我想创建一个大小为 1000000000 的数组。然后在数组的每个索引处,存储 int val,例如:arr[100000000] = 4 .

当我尝试这样做时,Netbeans 在这一行中显示了一个错误:

arr = new long[y+1]` and `arr[j] = 0` 

"Possible Lossy conversion from long to int". Here's my code:-


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;
}

最佳答案

数组索引是 Java 中的整数,编译器会建议您。所以最大数组大小是(大约)Integer.MAX_VALUE .对于更大的数组,您应该使用 ArrayList。

绕过这个污垢&快速

arr = new long[(int)y+1];  

关于java - 从 long 到 int 的可能有损转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21828099/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com