Glen,
If you're looking to eliminate the warning, then specify <Object> as
your container type:
public class MyQueue {
private ArrayList<Object> list;
public MyQueue() { list = new ArrayList<Object>();
public static void main(String[] args) { }
public void add(Object tmp) {
list.add(tmp);
}
public void print() {
for (int i=0;i<list.size();i++) {
System.out.println(list.get(i));
}
}
}
However, to make best use of Java 1.5 generics, tie it together with a
unified parameter type (T):
public class MyQueue<T> {
private ArrayList<T> list;
public MyQueue() {
list = new ArrayList<T>();
}
public static void main(String[] args) { }
public void add(T tmp) {
list.add(tmp);
}
public void print() {
for (int i=0;i<list.size();i++) {
System.out.println(list.get(i));
}
}
}
To use:
MyQueue<String> queue = new MyQueue<String>();
queue.add("Hello there.");
--Dan
Oliver Wong - 07 Mar 2006 18:16 GMT
> Glen,
>
[quoted text clipped - 14 lines]
> }
> }
I recommend that you do NOT just add <Object>, and instead simply ignore
the warning (though the best solution would be to spend a day or two
studying Java generics to understand the what the warning really means). If
you don't specify a container type, that's basically saying "I don't know
what container type I should use yet", which is fine, and is honest. If you
specify <Object>, you're saying "I know what the container type should be;
it should be Object", and so you might end up with incorrect code.
- Oliver