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 / March 2006

Tip: Looking for answers? Try searching our database.

New to java

Thread view: 
Glen Davie - 07 Mar 2006 01:45 GMT
Can someone explain what I have done wrong and how can it be corrected?

MyQueue.java:37: warning: [unchecked] unchecked call to add(E) as a member
of the raw type java.util.ArrayList
     list.add(tmp);
             ^

public class MyQueue
{
  private ArrayList list;
  public MyQueue()
  {
     list = new ArrayList();
  }

  public static void main(String[] args)   {   }

  public <T> void add(T tmp)
  {
     list.add(tmp);
  }

  public void print()
  {
     for(int i=0;i<list.size();i++)
     {
        System.out.println(list.get(i));
      }
  }
}
ddaglas@gmail.com - 07 Mar 2006 03:09 GMT
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


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



©2008 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.