Java 用最大值初始化大数组
如何初始化所有 Integer.MAXVALUE
大小为 1000 * 1000 * 1000 * 1000
的数组?
例如,我想让这个 int[][][][]dp = new int [1000][1000][1000][1000];
全部具有最大值,因为稍后我需要比较最小值。
我尝试过
int [] arr = new int arr[N];
Arrays.fill(arr,Integer.MAXVALUE);
,但它不适用于多维数组,有人可以帮忙吗?
How can I initialize an array of size 1000 * 1000 * 1000 * 1000
of all Integer.MAXVALUE
?
for example, I want to make this int[][][][]dp = new int [1000][1000][1000][1000];
all have max value as later I need to compare a minimum.
I tried
int [] arr = new int arr[N];
Arrays.fill(arr,Integer.MAXVALUE);
but it doesn't work with multidimensional arrays, can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须这样做才能填充多维数组:
但是,您将无法初始化
new int[1000][1000][1000][1000]
除非您至少3.64 TB 内存。更不用说如果你有那么多内存的话需要多长时间。You'll have to do this to fill your multi-dimensional array:
You won't however be able to initialize
new int[1000][1000][1000][1000]
unless you have at least 3.64 terabytes of memory. Not to mention how long that would take if you did have that much memory.您需要一些非常专业的东西,例如 Colt 来生成所谓的 稀疏矩阵。您需要稍微改变您的逻辑,而不是针对
Integer.MAX_VALUE
进行测试,而是测试某个位置是否存在某些内容(默认为 ZERO ),如果不存在则考虑它Integer.MAX_VALUE
并保留它。这假设您仅插入值为
< 的可能数据的一小部分。整数.MAX_VALUE
。You need something very specialized like Colt to generate what is called a Sparse Matrix. You need to alter your logic slightly, instead of testing against a
Integer.MAX_VALUE
you test to see if something exists at a location ( defaults to ZERO ), if it doesn't then consider itInteger.MAX_VALUE
and leave it alone.This assumes you only insert a fraction of the possible data with values
< Integer.MAX_VALUE
.fill 将需要数组和每个维度的填充值作为参数。说 fill(array, 0,0,0) 或在您的情况下 fill(array, maxValue, maxValue, maxValue)。
干杯,
fill will need as arguments the array and the values to fill per dimension. Say fill(array, 0,0,0) or in your case fill(array, maxValue, maxValue, maxValue).
Cheers,