开发者

Java initialize large array with Max value

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?


You'll have to do this to fill your multi-dimensional array:

for (int i = 0; i < dp.length; i++) {
    for (int j = 0; j < dp[i].length; j++) {
        for (int k = 0; k < dp[j].length; k++) {
            Arrays.fill(dp[i][j][k], Integer.MAX_VALUE);
        }
    }
}

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.


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 it Integer.MAX_VALUE and leave it alone.

This assumes you only insert a fraction of the possible data with values < Integer.MAX_VALUE.


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,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜