简体   繁体   English

尝试在Java中编写优先级队列,但获得“线程“ main”中的异常” java.lang.ClassCastException”

[英]Trying to write priority queue in Java but getting “Exception in thread ”main“ java.lang.ClassCastException”

For my data structure class, I am trying to write a program that simulates a car wash and I want to give fancy cars a higher priority than regular ones using a priority queue. 对于我的数据结构类,我正在尝试编写一个模拟洗车的程序,并且我想使用优先级队列为高档汽车提供比普通汽车更高的优先级。 The problem I am having has something to do with Java not being able to type cast "Object" as an "ArrayQueue" (a simple FIFO implementation). 我遇到的问题与Java无法将类型“ Object”转换为“ ArrayQueue”(简单的FIFO实现)有关。 What am I doing wrong and how can I fix it? 我在做什么错,我该如何解决?

public class PriorityQueue<E>
{

    private ArrayQueue<E>[] queues;
    private int highest=0;
    private int manyItems=0;


    public PriorityQueue(int h)
    {
        highest=h;
        queues = (ArrayQueue<E>[]) new Object[highest+1];   <----problem is here
    }


    public void add(E item, int priority)
    {
        queues[priority].add(item);
        manyItems++;
    }


    public boolean isEmpty( )
    {
        return (manyItems == 0);
    }


    public E remove()
    {
        E answer=null;
        int counter=0;

        do
        {
            if(!queues[highest-counter].isEmpty())
            {
                answer = queues[highest-counter].remove();
                counter=highest+1;
            }
            else
                counter++;
        }while(highest-counter>=0);

        return answer;
    }
}

EDIT 编辑

Thank you both for the quick answer to this question. 谢谢你们对这个问题的快速回答。 I solved the problem by following your advice and one other bit of code: 我按照您的建议和另一段代码解决了这个问题:

public PriorityQueue(int h)
{
    highest=h;
    queues = new ArrayQueue[highest+1];
    for(int i = 0; i <= highest; i++)
    {
        queues[i] = new ArrayQueue();
    }
}

An Object is an Object and (in most cases) not an ArrayQueue. 对象是一个对象,并且(在大多数情况下)不是ArrayQueue。 So indeed the cast is not possible. 因此确实无法进行演员表转换。

Creation of generic arrays is a problem too, but in your case, this should work: 创建通用数组也是一个问题,但是在您的情况下,这应该可行:

public PriorityQueue(int h)
{
    highest=h;
    queues = new ArrayQueue[highest+1];   // Gives an ignorable warning
}

EDIT 编辑

The way it is explained in your textbook is incorrect, the book needs a new revision cycle ;) The suggested cast is not allowed in Java, it's like an attempt to do 在教科书中解释的方式不正确,这本书需要一个新的修订周期;)Java中不允许使用建议的强制转换,就像尝试这样做一样

String forEverUseless = (String) new Object(); // this will not give an empty String
                                               // but an ouch-that-hurts-Exception

which is more obvious. 这更明显。 You can never cast a class to one of its subtypes (derived classes). 永远不能将类强制转换为其子类型之一(派生类)。 This is true for all classes, including arrays and generic classes. 对于所有类,包括数组和泛型类,都是如此。

EDIT 2 编辑2

Two more suggestions: 另外两个建议:

  1. The 'add' method should get a check whether 'priority' is in the valid range of priorities, otherwise add will throw an exception (like in: queue.add(entry, -1) ) 'add'方法应检查'priority'是否在优先级的有效范围内,否则add将引发异常(例如: queue.add(entry, -1)
  2. A remove method usually has an argument - you might want to call it with the element that shall be removed from the queue. remove方法通常具有一个参数-您可能希望使用应从队列中删除的元素来调用它。 (Or - if you're intention is something else, i suggest using the common queue operation names pop , push and peek ) (或者-如果您还有其他意图,我建议您使用常见的队列操作名称poppushpeek

The problem is almost exactly what you said -- you're making something of type Object[] and trying to cast it to ArrayQueue[] , and those aren't compatible types. 问题几乎与您所说的完全一样-您正在制作Object[]类型的某种东西,并试图将其ArrayQueue[]ArrayQueue[] ,而这些类型是不兼容的类型。 You should just do: 您应该这样做:

queues = new ArrayQueue[highest+1];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“ main”中的异常java.lang.ClassCastException:具有优先级队列和比较器 - Exception in thread “main” java.lang.ClassCastException: with priority queue and comparator 线程“ main”中的异常java.lang.ClassCastException: - Exception in thread “main” java.lang.ClassCastException: 线程“主”java.lang.ClassCastException 中的异常:setcollection.Enseignant 无法转换为 java.lang.Comparable - Exception in thread “main” java.lang.ClassCastException: setcollection.Enseignant cannot be cast to java.lang.Comparable 线程“ main”中的异常java.lang.ClassCastException:java.lang.Integer无法转换为q3.Box - Exception in thread “main” java.lang.ClassCastException: java.lang.Integer cannot be cast to q3.Box 线程“主”java.lang.ClassCastException 中的异常:proj.Car 无法转换为 java.lang.Comparable - Exception in thread "main" java.lang.ClassCastException: proj.Car cannot be cast to java.lang.Comparable 线程“ main”中的异常java.lang.ClassCastException:[Ljava.lang.Object; 无法转换为[B - Exception in thread “main” java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [B 线程“ main”中的异常java.lang.ClassCastException:[Ljava.lang.Object; 无法转换为模型。AdminPopulate - Exception in thread “main” java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to model.AdminPopulate 线程“main”中的异常 java.lang.ClassCastException:hello 不能转换为 java.awt.event.ActionListener - Exception in thread “main” java.lang.ClassCastException: hello cannot be cast to java.awt.event.ActionListener 每当我在 java 中使用 treeset.add() 方法时,线程“主”java.lang.ClassCastException 中的错误异常 - Error Exception in thread “main” java.lang.ClassCastException whenever I use treeset .add() method in java 无法使我的ArrayList成为线程安全的。 线程“主”中的异常java.lang.ClassCastException:怎么了? - Failed to make my ArrayList Thread-Safe. Exception in thread “main” java.lang.ClassCastException: What is wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM