源代码
代码中的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]那么找出来的最小值不是错的?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
找到数组中第一个不是null的元素下标,目的是确保在第二步中,minIndex所指示的数组元素不是null。
[1,null,3,4]能找出最小值,因为第二个循环里已经check过null了