java:无法推断MaxHeap的类型参数<&燃气轮机;
只是在堆东西的时候碰到了一些问题。我根据正在使用的接口限制了边界,并尝试访问一个有效添加堆的构造函数,以证明与通过插入添加堆相比,时间复杂度降低了。换句话说,我需要这个构造函数来工作,但是驱动程序不允许我将MaxHeapInterface对象初始化为整数。有什么想法吗java:无法推断MaxHeap的类型参数<&燃气轮机;,java,generics,data-structures,heap,max-heap,Java,Generics,Data Structures,Heap,Max Heap,只是在堆东西的时候碰到了一些问题。我根据正在使用的接口限制了边界,并尝试访问一个有效添加堆的构造函数,以证明与通过插入添加堆相比,时间复杂度降低了。换句话说,我需要这个构造函数来工作,但是驱动程序不允许我将MaxHeapInterface对象初始化为整数。有什么想法吗 import java.util.Arrays; public final class MaxHeap<T extends Comparable<? super T>> implements MaxHea
import java.util.Arrays;
public final class MaxHeap<T extends Comparable<? super T>> implements
MaxHeapInterface<T>
{
private T[] heap;
private int backIndex;
private static final int DEFAULT_CAP = 101;
private static final int MAX_CAP = 101;
private int efficientCounter=0;
private int counter=0;
public MaxHeap()
{
this(DEFAULT_CAP);
}
public MaxHeap(int chosenCap)
{
if(chosenCap < DEFAULT_CAP )
chosenCap = DEFAULT_CAP;
else
checkCapacity(chosenCap);
@SuppressWarnings("unchecked")
T[] holdHeap = (T[]) new Comparable[chosenCap +1];
heap = holdHeap;
backIndex = 0;
}
//efficient addition method-constructor
public MaxHeap(T[] entry)
{
this(entry.length);
for(int i =0; i < entry.length; i++)
heap[i+1] = entry[i];
for(int j = backIndex/2; j > 0; j-- )
{
reHeap(j);
}
}
//other addition method
public void add(T entry)
{
int index = backIndex+1;
int halfIndex = index/2;
counter++;
while((halfIndex > 0) && entry.compareTo(heap[halfIndex])>0)
{
heap[index] = heap[halfIndex];
index = halfIndex;
halfIndex = index/2;
counter++;
}
heap[index] = entry;
backIndex++;
}
public T removeMax()
{
T root = null;
if(!isEmpty())
{
root = heap[1];
heap[1] = heap[backIndex];
backIndex--;
reHeap(1);
}
return root;
}
public T getMax()
{
T root = null;
if(!isEmpty())
root = heap[1];
return root;
}
public boolean isEmpty()
{
return backIndex < 1;
}
public int getSize()
{
return backIndex;
}
public void clear()
{
while(backIndex > -1)
{
heap[backIndex] = null;
backIndex--;
}
backIndex = 0;
}
public int getEffcientcounter()
{
return efficientCounter;
}
public int getCounter()
{
return counter;
}
private void reHeap(int index)
{
boolean done = false;
T alone = heap[index];
int leftChildLocation = 2*index;
while(!done && (leftChildLocation <= backIndex))
{
int biggerChildLocation = leftChildLocation;
int rightChildLocation = leftChildLocation +1;
if((rightChildLocation <= backIndex)&& heap[rightChildLocation].compareTo(heap[biggerChildLocation])>0)
{
biggerChildLocation = rightChildLocation;
}
if(alone.compareTo(heap[biggerChildLocation])<0)
{
heap[index] = heap[biggerChildLocation];
index = biggerChildLocation;
leftChildLocation = index *2;
}
else{
done = true;
}
heap[index] = alone;
}
}
private void checkCapacity(int size)
{
if(size>MAX_CAP)
throw new IllegalStateException("Attempt to create a bag way too big." +
"\n the limit is "+ MAX_CAP);
}
public void printHeap()
{
for(int i = 0; i < heap.length; i++)
System.out.print(heap[i]+"," +" ");
}
导入java.util.array;
公共最终类MaxHeap原语和对象不会混合。问题在于:
int array = new int[101];
int array2 = new int[101];
当方法需要对象数组时,不能传入基元数组,所以只需将其改为对象数组即可
Integer[] array = new Integer[101];
Integer[] array2 = new Integer[101];
然后可以继续进行类型推断。如果可能的话,您可以把完整的代码放进去,因为它更像是一个独立的java程序。顺便说一句,我没有看到以数组作为参数的MaxHeap的构造函数。可以!我应该指定,带有(T[]entry)的泛型数据类型是数组的构造函数(至少我这么认为)您所说的MaxHeapInterface
是什么意思?您没有名为Array
的自定义类型。很抱歉,我最初使用的是Integer,一定是弄错了。非整数根本不起作用你的意思是“整数[]数组=新整数[101]”吗?因为它似乎起作用了,但谢谢你!。。。对我是那个意思。
int array = new int[101];
int array2 = new int[101];
Integer[] array = new Integer[101];
Integer[] array2 = new Integer[101];