이 프로그램에서 Java의 arraylists를 사용하여 힙 우선 순위 대기열을 생성합니다. 문제를 쉽게 해결하는 데 도움이되도록 코드를 계속 유지하려고 노력할 것입니다.기호를 찾을 수 없습니다/generics에서 ArrayList를 사용하여 비교 대상으로 객체를 변환 할 수 없습니다
본질적으로 heapAPI에 대한 인터페이스를 정의하고이를 Heap 클래스에 구현했습니다. 힙 생성자는 객체의 arraylist를 정의하여 힙 객체를 생성합니다. 여기서는 PCB 클래스의 객체 (우선 순위 대기열에 들어갈 작업)를 전달하려고합니다. 그러나 이러한 객체를 전달하면 arraylist를 통해 액세스 할 수 없습니다.
아래 첨부 된 코드는 HeapAPI, Heap 클래스 및 PCB 클래스입니다.
HeapAPI.java
public interface HeapAPI<E extends Comparable<E>>
{
boolean isEmpty();
void insert(E item);
E remove() throws HeapException;
E peek() throws HeapException;
int size();
}
Heap.java
public class Heap<E extends Comparable<E>> implements HeapAPI<E>
{
private ArrayList<E> tree;
public Heap()
{
tree = new ArrayList<>();
}
// don't believe the rest of the class is necessary to show
}
내가 ArrayList를 통해 PCB 객체의 함수를 호출하기 위해 다음과 같은 주요 방법을 시도
public class PCB implements Comparable<PCB>
{
private int priority;
// various private variables
public PCB()
{
priority = 19;
// instantiated variables
}
// don't believe the rest of the code is necessary
// the one specific function of PCB I use follows
public int getPriority()
{
return priority;
}
}
PCB.java PCB 객체를 Heap 객체의 arraylist에 삽입 한 후.
Main.java
public class Main
{
public static void main(String[] args) throws HeapException
{
Heap temp = new Heap();
PCB block = new PCB();
PCB block1 = new PCB();
PCB block2 = new PCB();
temp.insert(block);
temp.insert(block1);
temp.insert(block2);
block.getPriority();
// does not work
int num = temp.peek().getPriority();
//does not work
num = temp.get(0).getPriority();
}
나는 프로그램이 기호를 찾을 수 없다는 무엇입니까 오류 : 방법 getPriority을().
[import java.util.ArrayList; 각 파일에서 호출 됨]
나는 뜨거운 제록스에 대해 제네릭을 배우고 적용하려고 시도해 왔지만 지금은 막혀있다.
아무 것도 명확하지 않은 경우 쉽게 코드를 추가하거나 문제를 명확히 할 수 있습니다.
도움을 주시면 감사하겠습니다.
감사합니다.
PCB가 "자연적인"주문으로 간주되지 않는다면 (그리고 PCB가 "인쇄 회로 기판"을 의미하는 경우에는 그렇게 생각하지 않습니다.)대신에,'PCB'는'Comparable'을 구현해서는 안되며,'HeapAPI'는'Comparable' 타입으로 제한 할 필요가 없습니다; 오히려'HeapAPI' 구현 자의 생성자에'Comparator '을 제공 할 수 있습니다.이 구현자를 사용하면 요소를 정렬 할 수 있습니다. [이것에 대한 많은 질문/답변] (https://stackoverflow.com/questions/4108604/java-comparable-vs-comparator). –