views:

64

answers:

3

Hello. I am trying to make a FIFO Queue that is filled with my own class object.
I found this example but if I replace < E > with < PCB > it does not work:

import java.util.LinkedList;


public class SimpleQueue<E> {

private LinkedList<E> list = new LinkedList<E>();


 public void put(E o) {
    list.addLast(o);
     }


  public E get() {
     if (list.isEmpty()) {
          return null;
      }
   return list.removeFirst();
   }


   public Object[] getAll() {
     Object[] res = new Object[list.size()];
    for (int i = 0; i < res.length; i++) {
      res[i] = list.get(i);
      }
   list.clear();
    return res;
 }



    public E peek() {
      return list.getFirst();
      }


  public boolean isEmpty() {
     return list.isEmpty();
    }


  public int size() {
    return list.size();
    }
  }
+1  A: 

E is a type parameter. In simple terms, you can consider it as a 'template' which can be used to create a queue that can hold instances of one particular class.

You can create a queue of your PCB objects as follows:

SimpleQueue<PCB> queue = new SimpleQueue<PCB>();

Java Generics FAQs is a good resource if you want to learn more about Java generics.

Richard Fearn
I tried that! I said so in the actual question
Luron
@Luron: you said "...if I replace < E > with < PCB >..." which I took to mean that you actually replaced `<E>` with `<PCB>` in the Java source for `SimpleQueue`. If you instantiated `SimpleQueue` as in my answer, why does it not work? I've tried it and it seems to work (a `put` and `get` seem to work OK, but I may be missing something). Do you have a compilation error or stack trace?
Richard Fearn
no. as in type it in
Luron
@Luron: sorry, I don't understand. I've copied your `SimpleQueue` class into a new Java source file, and it compiles fine. I'm also able to instantiate it as shown in my answer. What problem do you have? A compilation error?
Richard Fearn
@Luron: So what is the error? Are you getting a compiler error message? Does it give some error when you run it? The code looks valid, and if Richard says he ran it and it works, than the problem is not blatant. You'll need to give us more information on the problem, not just "it didn't work".
Jay
A: 

Ignoring your immediate issue, are you aware that Java has a generic Queue already ? See the implementations linked from this interface specification.

Brian Agnew
A: 

The sun's generic tutorial says following:

We recommend that you use pithy (single character if possible) yet evocative names for formal type parameters. It’s best to avoid lower 3 case characters in those names, making it easy to distinguish formal type parameters from ordinary classes and interfaces. Many container types use E, for element, as in the examples above.

So, it can't be the problem that you changed it to PCB.

But if PCB is the only class of which you want to store objects, you don't have to create a generic class. Just remove <PCB> from your class definition line and replace all E's with PCB:

public class SimpleQueue
{
    LinkedList<PCB> list = new LinkedList<PCB>();

    ....

    public PCB peek()
    {
        return list.getFist();
    }
}
Martijn Courteaux
I said I tried that. and it didn't work.
Luron