1

Probably tons of mistakes, sorry. I'm new to java and I'm having a rather hard time with all the syntax and whatnot. I can't get past this error but I fail to see where it is. I can't even compile because I get the message: "...uses unchecked or unsafe operations. Note: Recompile with -Xlint: unchecked for details" Help, please.

This is supposed to implement the Queue interface which is pretty much just

public boolean enqueue(Item item);
public Item dequeue();
public int size;
public boolean isEmpty();
public boolean isFull();

I did try to make it circular but I don't know if I've succeeded. I think it's those generics that bring about the problem, i don't know.

    public class ArrayQueue<Item> implements Queue<Item> {
    private Item[] q;
    public int N = 0;
    private int tail = 0;
    private int head = 0;

    public ArrayQueue(int capacity) {
    q = (Item[]) new Object [capacity];
    }

    public boolean enqueue(Item item) {
        if (isFull())
            return false;
        q[tail++] = item;

        if(tail == q.length )
            tail = 0;
        N++;
        return true;
    }

    public Item dequeue( ) {
        if (isEmpty( ) )
            return null;
        N--;
        Item headItem = q[head];
        q[head++] = null;
        if(head == q.length )
            head = 0;

        return headItem;
    }



    public int size( ) {
        return N;
    }

    public boolean isFull() {
        return N == q.length;
    }


    public boolean isEmpty() {
        return N == 0;
    }
}
user2006562
  • 75
  • 1
  • 2
  • 9
  • 1
    This is not an error. You can live with it - "...uses unchecked or unsafe operations. Note: Recompile with -Xlint: unchecked for details" – Crickcoder Sep 24 '13 at 02:50
  • 1
    I think you have to implement all the Queue abstract methods – gjman2 Sep 24 '13 at 02:57
  • Yeah, I'm leaving it as is – user2006562 Sep 24 '13 at 03:11
  • Is it a requirement to implement the `Queue` interface? If not, make your own lightweight queue interface. You won't need to worry about generics then. Oh, and you may want to change your class declaration to `public class ArrayQueue implements Queue {...` – Meesh Sep 24 '13 at 03:26

2 Answers2

0

The warning about “unchecked or unsafe operations” should not prevent the code from being compiled. You have to fix the other errors. Note that it is impossible to implement array-based generic collections without getting such warnings (or suppressing them). That’s why it is better to use existing JFC Collection implementations as you can expect their developers to be careful and experienced enough to differentiate between unavoidable warnings and real problems.

Holger
  • 285,553
  • 42
  • 434
  • 765
0

This is not an error, that is just a warning because of one of the casting. you can use one these options: 1- Add Item[] ts = (Item[])(new Object[CAPACITY]); q = ts; or

2- add @SuppressWarnings("unchecked") at the beginning of the class to ignore all warning and see where the error is.