Avoiding unchecked warning

Hello eveyone. I'm relatively new on generics, and I'm curious about something. Consider this code:

...

Vector v = u.getDataVector();

for (Object elem : v){

Vector<String> st = (Vector<String>)elem;

if(Long.valueOf(st.elementAt(0)) == c.getId()){

// Do your things.

return;

}

}

It works just fine. v is a Vector made of Vector<String> items. Running it produces no problem whatsoever. But NetBeans keeps complaining that:

warning: [unchecked] unchecked cast

found: java.lang.Object

required: java.util.Vector<java.lang.String>

Vector<String> st = (Vector<String>)elem;

1 warning

If I change the loop with:

for (Vector<String> elem : v){

I get incompatible type (Object vs Vector<String>) error instead.

Now the question is: is there any "right" way to avoid the warking message?

Thanks in advance!

Fabio.

Message was edited by:

FabioFranchello

[1345 byte] By [FabioFranchelloa] at [2007-11-27 9:34:03]
# 1
You haven't told the compiler what's in your vector, v. You need to define v as:Vector<Vector><String>> v = ...which in turn may mean you need to change getDataVector() to avoid an unchecked warning on that line too...
dannyyatesa at 2007-7-12 22:57:16 > top of Java-index,Core,Core APIs...
# 2
**** Sun forum software. Ignore the first '>' character in my post
dannyyatesa at 2007-7-12 22:57:16 > top of Java-index,Core,Core APIs...
# 3

> You haven't told the compiler what's in your vector,

> v. You need to define v as:

> > Vector<Vector><String>> v = ...

>

> which in turn may mean you need to change

> getDataVector() to avoid an unchecked warning on that

> line too...

I was thinking about that, but sadly I can't change getDataVector() - it's from TableModel and it returns a raw Vector. :(

Thanks anyway!

FabioFranchelloa at 2007-7-12 22:57:16 > top of Java-index,Core,Core APIs...
# 4
Then there is no solution. You will have to have an unchecked warning somewhere. You can suppress it with the @SuppressWarnings annotation.
dannyyatesa at 2007-7-12 22:57:16 > top of Java-index,Core,Core APIs...