0

I am trying to recreate the built in method Offer() myself to use to add items(Strings) to an existing Array List.

Here is my code already:

import java.util.ArrayList;

public class PriorityQueue<item> {

    ArrayList<String> queue;

    public PriorityQueue() {
        this.queue = new ArrayList<String>();
    }

    public boolean offer(String item) {
        //Add to queue
       ensureCapacityInternal(size + 1);
       elementData[size++] = e;
       return true;
    }

    public static void main(String[] args) {
        PriorityQueue myQueue = new PriorityQueue();

        myQueue.offer("item1");

        System.out.println(myQueue);
    }
}

The issue is that when I run this I get multiple errors. The first one is:

eclipse-workspace/PriorityQueue/src/PriorityQueue.java:13:32
java: cannot find symbol
  symbol:   variable size
  location: class PriorityQueue<item>

The second one is:

eclipse-workspace/PriorityQueue/src/PriorityQueue.java:14:9
java: cannot find symbol
  symbol:   variable elementData
  location: class PriorityQueue<item>

I keep getting these "cannot find symbol". Why are they occuring and is there anything I can do to get this recreated offer/add method working?

Many thanks, From a coding newbie.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 2
    Where is `size` declared? – Federico klez Culloca Sep 02 '22 at 16:24
  • 1
    Does this answer your question? [What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-or-cannot-resolve-symbol-error-mean) – Federico klez Culloca Sep 02 '22 at 16:25
  • Mmm I just realised, no where! Let me try fix this. – CoderInNeed Sep 02 '22 at 16:25
  • 1
    As an aside, you're defining `PriorityQueue` as a generic class, but then you instantiate it like this: `PriorityQueue myQueue = new PriorityQueue();`. Please read [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Federico klez Culloca Sep 02 '22 at 16:27
  • Also, before wondering why you'll get the output you'll get when printing, please read [How to use the toString method in Java?](https://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java) – Federico klez Culloca Sep 02 '22 at 16:29
  • All of ensureCapacityInternal(size + 1); elementData[size++] = e; not declared – gapsf Sep 02 '22 at 16:29
  • Have to say that it's a slightly odd thing to do to roll your own collection classes using those from ```java.util``` . You *do* know that ```java.util.PriorityQueue``` exists..? – g00se Sep 03 '22 at 13:15

0 Answers0