我在“ NullPointerException”行中有一个newIndexer[i].index = (int)Double.POSITIVE_INFINITY;,但我不知道为什么。任何帮助深表感谢。

    public void resizeIndexer(int newKey)
    {
        if (maxHeap >= newKey)
            return;
        if (newKey > maxHeap){
            Handle[] newIndexer = new Handle[newKey + 1];
            for (int i = 0; i < newKey; i++){
                if (i < maxHeap)
                newIndexer[i] = this.Indexer[i];
                else{
                    System.out.println(i);
                    newIndexer[i].index = (int)Double.POSITIVE_INFINITY;
                    newIndexer[i].status = false;
                }
            }
            maxHeap = newKey;
            Indexer = newIndexer;
        }
    }

最佳答案

您必须在修改indexstatus成员之前为新数组的每个新索引创建一个新的Handle实例:

        for (int i = 0; i < newKey; i++){
            if (i < maxHeap)
                newIndexer[i] = this.Indexer[i];
            else{
                System.out.println(i);
                newIndexer[i] = new Handle ();
                newIndexer[i].index = (int)Double.POSITIVE_INFINITY;
                newIndexer[i].status = false;
            }
        }

09-25 20:36