Java initializes a large array with a maximum value

How can I initialize an array of size 1000 * 1000 * 1000 * 1000 for all Integer.MAXVALUE ?

For example, I want this int[][][][]dp = new int [1000][1000][1000][1000]; everything was the maximum value, since later I need to compare the minimum.

I tried

 int [] arr = new int arr[N]; Arrays.fill(arr,Integer.MAXVALUE); 

but it does not work with multidimensional arrays, can anyone help?

+4
source share
3 answers

You will need to do this to populate your multidimensional 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); } } } 

However, you cannot initialize new int[1000][1000][1000][1000] if you have at least 3.64 terabytes . Not to mention how long it will take if you have such a memory.

+12
source

You need something very specialized, such as Colt , to create what is called . You need to change your logic a bit, instead of testing the Integer.MAX_VALUE tag to check if something exists in the location (the default is ZERO) if it doesn't take Integer.MAX_VALUE into account and leave it alone.

It is assumed that you insert only part of the possible data with values < Integer.MAX_VALUE .

+2
source

fill, you must use an array and values ​​for each dimension as arguments. Say fill (array, 0,0,0) or in your case fill (array, maxValue, maxValue, maxValue).

Greetings

0
source

All Articles