为什么java需要花费很长时间来初始化二维数组,从第一个具有大尺寸数的维开始?

时间:2021-07-02 21:36:26

I have noticed that initializing 2D array like this

我注意到像这样初始化2D数组

case 1 :-

情况1 :-

int ar [] [] = new int [10000001][10] ;

taking more time than initializing it like this

花费更多的时间而不是像这样初始化它

case 2 :-

案例2: -

int ar[] [] = new int [10] [10000001] ;

in case 1 it toke time around 4000ms but in case 2 it does not exceed 100ms why there is this big gap ?

在案例1中,它的时间大约为4000毫秒,但在案例2中它不超过100毫秒,为什么会有这么大的差距?

1 个解决方案

#1


10  

Strictly speaking, Java does not have 2D arrays: instead, it uses 1D arrays arranged into 1D arrays of arrays.

严格地说,Java没有2D数组:相反,它使用排列成一维数组阵列的一维数组。

In your first case, in addition to the single array of arrays, Java makes 10000001 arrays of 10 elements, while in the second case it makes 10 arrays of 10000001 elements.

在第一种情况下,除了单个数组数组外,Java还生成10000个包含10个元素的数组,而在第二种情况下,它生成10个10000001个数组。

Since the number of objects differs by a factor of million, the first case is significantly slower.

由于对象的数量相差百万,因此第一种情况明显变慢。

#1


10  

Strictly speaking, Java does not have 2D arrays: instead, it uses 1D arrays arranged into 1D arrays of arrays.

严格地说,Java没有2D数组:相反,它使用排列成一维数组阵列的一维数组。

In your first case, in addition to the single array of arrays, Java makes 10000001 arrays of 10 elements, while in the second case it makes 10 arrays of 10000001 elements.

在第一种情况下,除了单个数组数组外,Java还生成10000个包含10个元素的数组,而在第二种情况下,它生成10个10000001个数组。

Since the number of objects differs by a factor of million, the first case is significantly slower.

由于对象的数量相差百万,因此第一种情况明显变慢。