Home arrow static arrow Java Programming [Archive] - Problem with unchecked/unsafe Vector use
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Problem with unchecked/unsafe Vector use
This topic has 16 replies on 2 pages.    1 | 2 | Next »

Posts:47
Registered: 5/1/02
Problem with unchecked/unsafe Vector use  
Jul 13, 2004 10:46 PM



 
i want to use
	public void addPart( Part part )		{ parts.addElement(part); }

but i get a
"warning: [unchecked] unchecked call to addElement(E) as a member of the raw type java.util.Vector"

plz tell me wtf that means
and how i get around it
 

Posts:197
Registered: 02.09.97
Re: Problem with unchecked/unsafe Vector use  
Jul 14, 2004 12:13 AM (reply 1 of 16)



 
You create a Vector-object without specifiying the type of data it will hold. So somewhere in your code there is the line
Vector parts=new Vector();

It was done this way until Generics where introduced in Java1.5.
Now with Java1.5 you can (should ?) also state the tyle of elements can store:
Vector parts<Part>=new Vector();

Now (Java1.5) every call to parts.addElement(type data) expects data to be an Object of type Part, while in the old days (Java1.4) every data of type Object was okay.

So what to do about the warning..
If you don't want to use that new features of Generics, you can either just ignore the warning, because it will not stop the compiler. But if you have a lot of this type of code, you can call javac with the commandline
javac -source 1.4 mypackage.MyClass.java

Then the compiler will not issue the warning any more.

 

Posts:47
Registered: 5/1/02
Re: Problem with unchecked/unsafe Vector use  
Jul 14, 2004 4:56 PM (reply 2 of 16)



 
ok thats totaly kool thx

but my object type is string
my code is now
	Vector tmp<String> = new Vector( 0, 1 );

and it tells me

Results.java:194: ';' expected
Vector tmp<String> = new Vector( 0, 1 );
 

Posts:2,206
Registered: 8/15/02
Re: Problem with unchecked/unsafe Vector use  
Jul 14, 2004 5:16 PM (reply 3 of 16)



 
You know, I really liked type unsafe Vectors! Throw in any old class and it just worked. Course you had to do some work when getting things out!
 

Posts:197
Registered: 02.09.97
Re: Problem with unchecked/unsafe Vector use  
Jul 14, 2004 11:03 PM (reply 4 of 16)



 
Results.java:194: ';' expected
Vector tmp<String> = new Vector( 0, 1 );

And now you are using a feature of Java1.5, so that commandline switch needs to be -source 1.5 to make it compile. (Or you leave out the source-switch, because the compiler uses 1.5 as default.)

You have to decide :
use the new features and get a warning for each line that uses the old style
or
go for Java 1.4 in which case the Generics won't compile.
 

Posts:18,384
Registered: 21.03.00
Re: Problem with unchecked/unsafe Vector use  
Jul 14, 2004 11:19 PM (reply 5 of 16)



 
Hi,

I'm not sure, but shouldn't it be...

Vector<String> tmp = new Vector<String>( 0, 1 );


And then compile with 1.5

/Kaj
 

Posts:47
Registered: 5/1/02
Re: Problem with unchecked/unsafe Vector use  
Jul 15, 2004 6:23 AM (reply 6 of 16)



 
yes thx kajbj
thats it!!!!!!!!!!!!!!!!!!!

i was already compiling with 1.5 and got the error

thx for the help guys
 

Posts:47
Registered: 5/1/02
Re: Problem with unchecked/unsafe Vector use  
Aug 6, 2004 1:45 PM (reply 7 of 16)



 
			input = new ObjectInputStream( new FileInputStream( "settings.dat" ) );			settings = ( Vector )input.readObject();

ok now i have this code

and this warning
found : java.util.Vector
required: java.util.Vector<java.lang.String>
settings = ( Vector )input.readObject();

if i put ( Vector<String> ) the warning
found : java.lang.Object
required: java.util.Vector<java.lang.String>
settings = ( Vector )input.readObject();

any ideas??
 

Posts:826
Registered: 7/1/03
Re: Problem with unchecked/unsafe Vector use  
Aug 6, 2004 1:49 PM (reply 8 of 16)



 
...and they said generics were "making life easy" for production sigh.
 

Posts:6,750
Registered: 1/25/04
Re: Problem with unchecked/unsafe Vector use  
Aug 6, 2004 2:01 PM (reply 9 of 16)



 
if i put ( Vector<String> ) the warning
found : java.lang.Object
required: java.util.Vector<java.lang.String>
settings = ( Vector )input.readObject();

You sure? Look at the last line of that error message - the compiler seems to think your code still says (Vector) and not (Vector<String>).
 

Posts:47
Registered: 5/1/02
Re: Problem with unchecked/unsafe Vector use  
Aug 6, 2004 5:01 PM (reply 10 of 16)



 
yes im sure

but i miss typed it here...
the last line should be
settings = ( Vector<String> )input.readObject():
 

Posts:5,965
Registered: 5/17/03
Re: Problem with unchecked/unsafe Vector use  
Aug 6, 2004 9:03 PM (reply 11 of 16)



 
You know, I really liked type unsafe Vectors! Throw
in any old class and it just worked. Course you had
to do some work when getting things out!

Can't you still do that if you specify Object as type?
 

Posts:179
Registered: 7/30/04
Re: Problem with unchecked/unsafe Vector use  
Aug 7, 2004 2:00 AM (reply 12 of 16)



 
Would it be a thought to just create a
class OVector extends Vector<Object>
?
 

Posts:47
Registered: 5/1/02
Re: Problem with unchecked/unsafe Vector use  
Aug 7, 2004 5:15 PM (reply 13 of 16)



 
is that the only way??
 

Posts:2,522
Registered: 3/6/01
Re: Problem with unchecked/unsafe Vector use  
Aug 8, 2004 7:55 PM (reply 14 of 16)



 
So this code:
			input = new ObjectInputStream( new FileInputStream( "settings.dat" ) ); settings = ( Vector<String> )input.readObject();
Yields this error?
 found   : java.lang.Object
required: java.util.Vector<java.lang.String>
settings = ( Vector<String>)input.readObject();
That sounds pretty messed up to me, though I'm not real familiar with generics.
 
This topic has 16 replies on 2 pages.    1 | 2 | Next »