简体   繁体   English

无法将对象添加到优先级队列

[英]Can't add objects to priority queue

I need to add a Jedi object to a queue.我需要将绝地对象添加到队列中。 None of the add methods that I know are working and all give the "cannot find symbol - method add(jedi1).I didn't learn much about Queues in my last class so I'm not exactly sure what I'm doing. Other things to note are 3 specifics of this.我知道的所有 add 方法都没有工作,并且都给出了“找不到符号 - 方法 add(jedi1)”。我在上一堂课中没有学到太多关于队列的知识,所以我不确定我在做什么。其他需要注意的是 3 个细节。

1)Modify the heap operations to keep the maximum – rather than the minimum – element in the root. 1) 修改堆操作以保持根中的最大元素而不是最小元素。 Refer to the Heap lab assignment for the algorithm.有关算法,请参阅堆实验室作业。

2) Utilize an array with a varying size limit (if <10% utilization reduce it by half; if full, double the size). 2) 使用具有不同大小限制的数组(如果 <10% 利用率将其减少一半;如果已满,则将大小增加一倍)。

3) Implement the heap to receive any generic object with a comparable method. 3) 使用可比较的方法实现堆以接收任何通用对象。

4) Allow for duplicate value with the following rule: If a duplicate value is entered, the new element with similar value should be considered to have less priority than an existing element. 4) 允许重复值遵循以下规则:如果输入重复值,则应认为具有相似值的新元素的优先级低于现有元素。

    public class PriorityQueue<JediQ>
    {
        Scanner reader = new Scanner(System.in);
        public void main(String[] args)
    {
       while (true)
    {
        System.out.println("Please select an option");
        System.out.println("1 - Add a Jedi ");
        System.out.println("2 - Remove an element");
        System.out.println("3 - Print head value");
        System.out.println("4 - Compare value to head value");
        System.out.println("5 - Print Array");
        System.out.println("6 - Exit");
        int x = 0;
        x = reader.nextInt();

        if (x == 1)
        {
            PriorityQueue<JediQ> line = new PriorityQueue<JediQ>();
            System.out.println("Enter the name of the Jedi");
            String Name = reader.next();
            System.out.println("Enter the midi count");
            double midi = reader.nextInt();

            Jedi jedi1 = new Jedi(Name, midi);

            line.add(jedi1);

            System.out.print("Jedi was added");
        }
        if (x == 6)
        {
            System.exit(0);
        }
    }
}

} }

Your problem is a vast lack of basic knowledge about everything;你的问题是对所有事情都缺乏基本的知识; so I give you some things to start with:所以我给你一些事情开始:

First of all, that error message means: your implementation of that PriorityQueue ... does not have an add() method.首先,该错误信息是指:说的实现PriorityQueue ......具有add()方法。 Because: you didn't write it!因为:你没有写! So you start with something like:因此,您可以从以下内容开始:

public class PriorityQueue<T> {
  public void add(T newElement) { ...

Which leads to the second major problem: the usage of generic types.这导致了第二个主要问题:泛型类型的使用。 As you can see in my example, you say: "my queue class, should be accepting any kind of object".正如您在我的示例中看到的,您说:“我的队列类应该接受任何类型的对象”。 And only later on, when instantiating a queue object;并且仅在稍后实例化队列对象时; then you declare that this instance should be for Jedis, like:然后你声明这个实例应该用于 Jedis,比如:

PriorityQueue<Jedi> jedis = new PriorityQueue<>();
jedies.add(lukeSkywalkerHisUnknownCousin);

But of course, the real fun within this exercise is the implementation of a priority queue, so that it provides all the methods that a queue should have;但是当然,这个练习中真正有趣的是优先级队列的实现,它提供了队列应该具有的所有方法; and that they work as outlined in your assignment!并且它们按照您的任务中的概述工作! And obviously, that is your assignment, so I leave that as exercise to the reader!显然,这是你的任务,所以我把它作为练习留给读者!

Final hint: if you want to understand the methods you ought to implement, you can have a look at java's own PriorityQueue and its methods!最后提示:如果你想了解你应该实现的方法,你可以看看java自己的PriorityQueue及其方法!

Use below method.使用下面的方法。

   line.offer(yourObject);

Please read the basics of collections framework.请阅读集合框架的基础知识。 You able to add/ modify / delete objects out of any collections because the wrapper classes have provided the methods for that.您可以从任何集合中添加/修改/删除对象,因为包装类已经为此提供了方法。 For eg.例如。 When you add int ( that becomes Integer as collection only stores Object).当您添加 int (由于集合仅存储对象而变为 Integer 时)。 the default Comparator is used.使用默认比较器。 as below:如下:

    public class PriorityQueueExample
    {
        public static void main(String[] args)
        {
            //Creating a PriorityQueue with default Comparator.

            PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>();

            //Inserting elements into pQueue.

            pQueue.offer(21);

            pQueue.offer(17);

            //Removing the head elements

            System.out.println(pQueue.poll()); 

To add custom objects in any collection you need to use either Comparator or Comparable.要在任何集合中添加自定义对象,您需要使用 Comparator 或 Comparable。

   class JediQ
        {
            String name;

            int midi;

            //Constructor Of JediQ

            public JediQ(String name, int midi)
            {
                this.name = name;

                this.midi = midi;
            }

            @Override
            public String toString()
            {
                return name+" : "+midi;
            }
        }

   and then adding the object as below:

            class MyComparator implements Comparator<JediQ>
        {
            @Override
            public int compare(JediQ e1, JediQ e2)
            {
                return e1.name - e2.name;
            }
        }


        MyComparator comparator = new MyComparator();

        PriorityQueue<JediQ> pQueue = new PriorityQueue<JediQ>(7, comparator);
        pQueue.offer(new JediQ("AAA", 150));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM