源代码
代码中的findMinIndex()方法如下:
private int findMinIndex( )
{
int i;
int minIndex;
for( i = 0; theTrees[ i ] == null; i++ )
;
for( minIndex = i; i < theTrees.length; i++ )
if( theTrees[ i ] != null &&
theTrees[ i ].element.compareTo( theTrees[ minIndex ].element ) < 0 )
minIndex = i;
return minIndex;
}
当中的第一个循环作用跟目的是什么?如果数组如[1,null,3,4]那么找出来的最小值不是错的?
Find the first element subscript in the array that is not null. The purpose is to ensure that in the second step, the array element indicated by minIndex is not null.
[1,null,3,4] can find the minimum value because null has been checked in the second loop