Java generic

M

mark13.pl

Hello,

I have a class:
public class MyList<T> implements MyListInterface<T> {
public void add(T elem) { ... }
public T get(int i) { ... }
}

In main program I create:

MyList[] list = new MyList[10];
for (i = 0; i < 10; i++) {
list = new LinkedList<Data>();
list.add(data);
}

getting a warning: "Type safety: The method add(Object) belongs to the
raw type MyList. References to generic type MyList<T> should be
parametrized."

Could you expain me how can I handle with that warning??

Regards, mark
 
D

Daniel Dyer

Hello,

I have a class:
public class MyList<T> implements MyListInterface<T> {
public void add(T elem) { ... }
public T get(int i) { ... }
}

In main program I create:

MyList[] list = new MyList[10];
for (i = 0; i < 10; i++) {
list = new LinkedList<Data>();
list.add(data);
}

getting a warning: "Type safety: The method add(Object) belongs to the
raw type MyList. References to generic type MyList<T> should be
parametrized."

Could you expain me how can I handle with that warning??


MyList<Data>[] list = new MyList<Data>[10]

Dan.
 
T

Thomas Hawtin

Daniel said:
Could you expain me how can I handle with that warning??

MyList<Data>[] list = new MyList<Data>[10]

Not going to work.

The "main program" should be something like:

List<MyList<Data>> list = new ArrayList<MyList<Data>>();
for (Data element : data) {
LinkedList<Data> sublist = new LinkedList<Data>(); // not java.util
sublist.add(element);

list.add(sublist);
}

With generics, you can (mostly) leave reference arrays behind.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top