Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Java generic

Thread view: 
mark13.pl@gmail.com - 19 Sep 2006 19:19 GMT
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[i] = new LinkedList<Data>();
    list[i].add(data[i]);
}

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
Daniel Dyer - 19 Sep 2006 19:40 GMT
> Hello,
>
[quoted text clipped - 17 lines]
>
> Could you expain me how can I handle with that warning??

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

Dan.

Signature

Daniel Dyer
http://www.dandyer.co.uk

Thomas Hawtin - 19 Sep 2006 20:08 GMT
>> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Daniel Dyer - 19 Sep 2006 20:26 GMT
>>> Could you expain me how can I handle with that warning??
>>  MyList<Data>[] list = new MyList<Data>[10]
>
> Not going to work.

Yep, forgot about that.  You'd have to use an unchecked cast, but your  
suggestion to use lists is probably more suitable.

Dan.

Signature

Daniel Dyer
http://www.dandyer.co.uk



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.