Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 811
CosmoRied Avatar asked Dec 10 '22 09:12

CosmoRied


1 Answers

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.

like image 147
WhiteFang34 Avatar answered Dec 11 '22 22:12

WhiteFang34