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 / First Aid / June 2005

Tip: Looking for answers? Try searching our database.

why get a warning while using the Vector's addElement()?

Thread view: 
iherage@gmail.com - 21 Jun 2005 04:11 GMT
This is part of the code(JDK 1.5 ,windows xp Chinese(simplified))
...

Vector v=new Vector();
v.addElement(new Integer(7));
...

I got a warning.Because I am using a Chinese version,it perhaps means
in English its type is not checked while using the method addElement,so
it is not safe to use it.Why?Is not Integer an Object?And what is the
right way?
Ingo R. Homann - 21 Jun 2005 08:07 GMT
Hi,

> This is part of the code(JDK 1.5 ,windows xp Chinese(simplified))
> ...
[quoted text clipped - 7 lines]
> it is not safe to use it.Why?Is not Integer an Object?And what is the
> right way?

The exact warning (in English) would be nice. I guess, you are using
Java5. Try this...

Vector<Integer> v=new Vector<Integer>();
v.addElement(new Integer(7));

...and read something about generics.

Hth,
Ingo
iherage@gmail.com - 21 Jun 2005 19:11 GMT
I read the java api doc of jdk 5.0 but it does not say anything about
it.It only says that the parameter is an element.
What about the other collections,LinkedList and ArrayList for
example?Do they have to use in this way?
Eric Olander - 21 Jun 2005 22:59 GMT
Generics were added to all the collection classes in 1.5.  Check out
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

-Eric

>I read the java api doc of jdk 5.0 but it does not say anything about
> it.It only says that the parameter is an element.
> What about the other collections,LinkedList and ArrayList for
> example?Do they have to use in this way?
RC - 22 Jun 2005 16:25 GMT
> Hi,
>
[quoted text clipped - 17 lines]
>
> ...and read something about generics.

Great! This is very helpful to me.
But I have different situations.

What happen if I have lines:

Vector v = (Vector)vector.elementAt(i);
v.addElement(new Integer(7));

What should I do?

I wrote:

Vector<Integer> v = (Vector)vector.elementAt(i);

I still got warning

java:778: warning: [unchecked] unchecked conversion
found   : java.util.Vector
required: java.util.Vector<java.lang.String>

Where I should put 2nd <Integer>?

Thank Q very much in advance!
Ingo R. Homann - 23 Jun 2005 07:48 GMT
Hi RC,

>> The exact warning (in English) would be nice. I guess, you are using
>> Java5. Try this...
[quoted text clipped - 27 lines]
>
> Thank Q very much in advance!

So, "v" is a Vector of Integers, and "vector" is a Vector of "Vector of
Integers"s? Then write this!

Vector<Integer> v;
Vector<Vector<Integer>> vector=...;

v=vector.elementAt(i);
v.addElement(7);

...and read something about "generics"!

Hth,
Ingo

PS: By the way, use ArrayList instead of Vector.


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.