here is a part of my code where I am getting error:
这是我的代码的一部分,我收到错误:
long p=1000000000-size-1;
long j;
for(j=p;j>p-k;j--)
{
sum2=sum2+sum[j];
}
System.out.print(sum2);
I think it is because I am using a long
variable to define the size of array. How could I deal with this error
我认为这是因为我使用一个长变量来定义数组的大小。我怎么能处理这个错误
Or if I am wrong please tell me how could I declare an array containing 10^10 elements.
或者,如果我错了,请告诉我如何声明包含10 ^ 10个元素的数组。
3 个解决方案
#1
Java does not provide a way to create arrays with size bigger than Integer.MAX_VALUE
(which is about 2*10^9). Also are you sure you have enough memory to store such array? If you want to store array of 10^10 int values, you will need at least 40 Gb of RAM.
Java没有提供创建大小大于Integer.MAX_VALUE(大约2 * 10 ^ 9)的数组的方法。你还确定你有足够的内存来存储这样的阵列吗?如果要存储10 ^ 10个int值的数组,则需要至少40 Gb的RAM。
#2
Change both the variables to int.
If the values don't fit into an int
the code won't work anyway.
将两个变量都更改为int。如果值不适合int,则代码无论如何都不会起作用。
I think it is because I am using a long variable to define the size of array. How could I deal with this error
我认为这是因为我使用一个长变量来定义数组的大小。我怎么能处理这个错误
No it isn't, because you aren't. You can't.
不,不是,因为你不是。你不能。
#3
Aside from the the maximum array concerns and availability of enough virtual memory: You have an integer literal 1000000000
, but what you actually want is a long literal 1000000000L
.
除了最大的数组关注点和足够的虚拟内存可用性:你有一个整数字面积1000000000,但你真正想要的是一个长文字1000000000L。
#1
Java does not provide a way to create arrays with size bigger than Integer.MAX_VALUE
(which is about 2*10^9). Also are you sure you have enough memory to store such array? If you want to store array of 10^10 int values, you will need at least 40 Gb of RAM.
Java没有提供创建大小大于Integer.MAX_VALUE(大约2 * 10 ^ 9)的数组的方法。你还确定你有足够的内存来存储这样的阵列吗?如果要存储10 ^ 10个int值的数组,则需要至少40 Gb的RAM。
#2
Change both the variables to int.
If the values don't fit into an int
the code won't work anyway.
将两个变量都更改为int。如果值不适合int,则代码无论如何都不会起作用。
I think it is because I am using a long variable to define the size of array. How could I deal with this error
我认为这是因为我使用一个长变量来定义数组的大小。我怎么能处理这个错误
No it isn't, because you aren't. You can't.
不,不是,因为你不是。你不能。
#3
Aside from the the maximum array concerns and availability of enough virtual memory: You have an integer literal 1000000000
, but what you actually want is a long literal 1000000000L
.
除了最大的数组关注点和足够的虚拟内存可用性:你有一个整数字面积1000000000,但你真正想要的是一个长文字1000000000L。