Hi, im just doing some past exam papers for an exam I've got coming up and im struggling with Java generics, i cant seem to find any good examples to help me out. I've found small snippets of code but i really need a larger piece of code shown before and after generic implementation so that i can understand properly what is going on. Anyway if no one can provide this i do have a piece of code which wuld be nice if you could implement gerneric code into and explain what you did.

Example code:

public class Queue {
private int[] items = new int[5];
private int top = 0;
public boolean isEmpty() {
return top <= 0;
}
public int get() {
int first = items[0];
for (int i = 0; i+1 < top; i++) {
items[i] = items[i+1];
}
top--;
return items[0];
}
public void add(int i) {
items[top++] = i;
}
public void remove(int i) {
if (i <= 0) return;
get();
remove(i - 1);
}
public static void main(String[] args) {
Queue s = new Queue();
s.add(3);
s.remove(2);
}
}

Thanks,

~Crag

Edited 4 Years Ago by Cragsterboy

That's a queue of ints, where generics won't do anything for you. But suppose you have the same thing but you want to store any kind of info. Before generics you would make everything an Object, eg private Object[] items = new Object[5];
Then if you wanted to store Strings you would just store them, and every time you retrieved one you would have to cast it back to String. That's dangerous because there's no check that you really did only store Strings, so the cast could blow up at run time (apart from making the code more tedious).
With generics you allow the user to specify what kind of objects will go in the queue, so they will create a Queue as (eg) new Queue<String>() or new Queue<Runnable>() Now the compiler can check that you are only adding valid items, and you don't need to cast any retrieved values because the compiler knows their type.
This is a good introduction, and this is also pretty easy.

An example/tutorial of generic programming is here. From there, you should be able to implement a non-generic programming.

okay thanks for the reply, so with my code i have given how would i modify it to use a generic type variable E so that the values stored in the queue all belong to the class E?

Thanks,

~Crag

Generic programming is not easy and not everyone could do. However, you should read on line for how to do it.

In your case, you could start with public class Queue<E> { ... }. The next part is to convert each private variable and method to generic type. That's the hard part. I am not here to code it for you, so you need to at least show your effort on this part first (code it, compile it, and let us know what error you are getting that you can't solve.)

okay I've read over the links you have given me and gave it ago,but i think I've done the integers wrong :/ Would it be like this:

public class Queue<E> {
private E[] items = new E[5];
private E top = 0;
public boolean isEmpty() {
return top <= 0;
}
public E get() {
E first = items[0];
for (E i = 0; i+1 < top; i++) {
items[i] = items[i+1];
}

Is that looking right or have i just done it completly wrong? :S

thanks,

~Crag

That's heading in the right direction :)

new E[5] is what we would all want to code there, but for arcane technical reasons you can't do that (google Java generics type erasure if you want to know why). So you have to create an Object array and bury the cast to (E) in your get methods to avcoid confusing the users.
If top is a ref to the top element in the queue then it can't be zero (zero is an integer value, but E may be String, Thread, anything). Initially top should be null (because there is no top element to refer to)
I don't understand the get method. It has a return type, but no return statement (= compile error) What is it supposed to return?

The Java compiler would have pointed out some of these problems in less time than it took me to type this.

To add to James comment about get() method, I think it is an error from the original code with regular Queue (look at the 1st post). Also, the variable type for i of the loop should still be integer. It is a local variable for iterating through array index and has nothing to do with the item in the queue.

This article has been dead for over six months. Start a new discussion instead.