how to solve this warning in Eclipse?

I use multiple vectors in certain parts of my program and I get the
following warning when adding stuff to the vector. Can anyone tell me
what it means and give an example of how I can get rid of the warnings?

Type safety: The method addElement(Object) belongs to the raw type
Vector. References to generic type Vector<E> should be parameterized

Advertisements

"Brandon McCombs" <> wrote in message
news:C52Rf.24975$...
>I use multiple vectors in certain parts of my program and I get the
>following warning when adding stuff to the vector. Can anyone tell me what
>it means and give an example of how I can get rid of the warnings?
>
>
> Type safety: The method addElement(Object) belongs to the raw type Vector.
> References to generic type Vector<E> should be parameterized
>
I assume you are using a Java 1.5 (or 1.6) compiler; Java 1.5 is the version
that introduced 'generics'.

The basic idea of generics is to introduce several features that encourage
type safety in your code. Type safety has been present in languages like C++
for years but were largely missing from Java until 1.5. I can't explain
generics very well but there are some articles about it in the 1.5
documentation. Look for it under the list of features that are new to 1.5.

Basically, to stop your warnings, you need to tell Java what sort of Object
you are going to store in your Vectors. For example, if you are putting
Strings in your Vector, use this:

Advertisements

Rhino wrote:
> "Brandon McCombs" <> wrote in message
> news:C52Rf.24975$...
>> I use multiple vectors in certain parts of my program and I get the
>> following warning when adding stuff to the vector. Can anyone tell me what
>> it means and give an example of how I can get rid of the warnings?
>>
>>
>> Type safety: The method addElement(Object) belongs to the raw type Vector.
>> References to generic type Vector<E> should be parameterized
>>
> I assume you are using a Java 1.5 (or 1.6) compiler; Java 1.5 is the version
> that introduced 'generics'.
>
> The basic idea of generics is to introduce several features that encourage
> type safety in your code. Type safety has been present in languages like C++
> for years but were largely missing from Java until 1.5. I can't explain
> generics very well but there are some articles about it in the 1.5
> documentation. Look for it under the list of features that are new to 1.5.
>
> Basically, to stop your warnings, you need to tell Java what sort of Object
> you are going to store in your Vectors. For example, if you are putting
> Strings in your Vector, use this:
>
> Vector<String> myVector = new Vector<String>();
>
> --
> Rjhino
>
>

Hey Rhino,

Thanks for your help on that last one. I'm in the middle of fixing all
those warnings. Your code helped.

Another thing I ran into is something similar but with Hashtables. I get
Type safety: The method put(Object, Object) belongs to the raw type
Hashtable. References to generic type Hashtable<K,V> should be parameterized

Rhino wrote:
> "Brandon McCombs" <> wrote in message
> news:C52Rf.24975$...
>> I use multiple vectors in certain parts of my program and I get the
>> following warning when adding stuff to the vector. Can anyone tell me what
>> it means and give an example of how I can get rid of the warnings?
>>
>>
>> Type safety: The method addElement(Object) belongs to the raw type Vector.
>> References to generic type Vector<E> should be parameterized
>>
> I assume you are using a Java 1.5 (or 1.6) compiler; Java 1.5 is the version
> that introduced 'generics'.
>
> The basic idea of generics is to introduce several features that encourage
> type safety in your code. Type safety has been present in languages like C++
> for years but were largely missing from Java until 1.5. I can't explain
> generics very well but there are some articles about it in the 1.5
> documentation. Look for it under the list of features that are new to 1.5.
>
> Basically, to stop your warnings, you need to tell Java what sort of Object
> you are going to store in your Vectors. For example, if you are putting
> Strings in your Vector, use this:
>
> Vector<String> myVector = new Vector<String>();
>
> --
> Rjhino
>
>

I have one last line that I can't fix the warning for.

allAttribsAndVals.get(i).add( newField.getText() );

allAttribsAndVals is declared as Vector<Vector> and the Vectors that it
holds are declared as Vector<String>. No matter what I do I can't get
the warning to go away. Any ideas?

"Brandon McCombs" <> wrote in message
news:c74Rf.24989$...
> Rhino wrote:
>> "Brandon McCombs" <> wrote in message
>> news:C52Rf.24975$...
>>> I use multiple vectors in certain parts of my program and I get the
>>> following warning when adding stuff to the vector. Can anyone tell me
>>> what it means and give an example of how I can get rid of the warnings?
>>>
>>>
>>> Type safety: The method addElement(Object) belongs to the raw type
>>> Vector. References to generic type Vector<E> should be parameterized
>>>
>> I assume you are using a Java 1.5 (or 1.6) compiler; Java 1.5 is the
>> version that introduced 'generics'.
>>
>> The basic idea of generics is to introduce several features that
>> encourage type safety in your code. Type safety has been present in
>> languages like C++ for years but were largely missing from Java until
>> 1.5. I can't explain generics very well but there are some articles about
>> it in the 1.5 documentation. Look for it under the list of features that
>> are new to 1.5.
>>
>> Basically, to stop your warnings, you need to tell Java what sort of
>> Object you are going to store in your Vectors. For example, if you are
>> putting Strings in your Vector, use this:
>>
>> Vector<String> myVector = new Vector<String>();
>>
>> --
>> Rjhino
>
> Hey Rhino,
>
> Thanks for your help on that last one. I'm in the middle of fixing all
> those warnings. Your code helped.
>
> Another thing I ran into is something similar but with Hashtables. I get
> Type safety: The method put(Object, Object) belongs to the raw type
> Hashtable. References to generic type Hashtable<K,V> should be
> parameterized
>
> Is the fix similar for this as it was for the Vector?

Yes, it's the same idea as for Vectors except that you need two parameters
in the angle brackets; the first one tells you the type of the Key (K) and
the second one tells you the type of the Value (V). So if you have a
HashTable where the Key is an Integer and the Value is a String, you'll need
something like this:

"Brandon McCombs" <> wrote in message
news:xv5Rf.24994$...
> Rhino wrote:
>> "Brandon McCombs" <> wrote in message
>> news:C52Rf.24975$...
>>> I use multiple vectors in certain parts of my program and I get the
>>> following warning when adding stuff to the vector. Can anyone tell me
>>> what it means and give an example of how I can get rid of the warnings?
>>>
>>>
>>> Type safety: The method addElement(Object) belongs to the raw type
>>> Vector. References to generic type Vector<E> should be parameterized
>>>
>> I assume you are using a Java 1.5 (or 1.6) compiler; Java 1.5 is the
>> version that introduced 'generics'.
>>
>> The basic idea of generics is to introduce several features that
>> encourage type safety in your code. Type safety has been present in
>> languages like C++ for years but were largely missing from Java until
>> 1.5. I can't explain generics very well but there are some articles about
>> it in the 1.5 documentation. Look for it under the list of features that
>> are new to 1.5.
>>
>> Basically, to stop your warnings, you need to tell Java what sort of
>> Object you are going to store in your Vectors. For example, if you are
>> putting Strings in your Vector, use this:
>>
>> Vector<String> myVector = new Vector<String>();
>>
>> --
>> Rjhino
>
> I have one last line that I can't fix the warning for.
>
> allAttribsAndVals.get(i).add( newField.getText() );
>
> allAttribsAndVals is declared as Vector<Vector> and the Vectors that it
> holds are declared as Vector<String>. No matter what I do I can't get the
> warning to go away. Any ideas?

Well, you haven't specified the text of the warning you are getting for this
line so that makes it a little hard ;-)

Looking at the API for Vector, it seems that this version of the add()
method is expecting a value of type 'E' but I'm still rocky on the notation
so I'm not completely sure what 'E' means in this context. You haven't said
what type 'newField' is but I'm guessing that its a JTextField or JTextArea,
which means it is using the getText() method from JTextComponent, which
returns a String: that should work fine. But there are many other getText()
methods, some of which don't return Strings. Look up the API for the type of
'newField' then see what it's getText() method is returning; if it's not a
String, that is likely your problem. Ultimately, I think the add() method
will have to pass a String to the Vector otherwise you'll continue to the
get the warning.

If newField.getText() _is_ returning a String, I'm not sure what the problem
is. In that case, perhaps someone else with better understanding of Generics
can help you.

Or maybe you'd like to wade through the Generics tutorial yourself so that
you can figure it out on your own? The main Generics tutorial is athttp://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf but parts of it are
over my head since my theory isn't that strong. I think there's a more basic
tutorial as well but I don't remember where I saw it.

Brandon McCombs wrote:
> allAttribsAndVals.get(i).add( newField.getText() );
>
> allAttribsAndVals is declared as Vector<Vector> and the Vectors that it
> holds are declared as Vector<String>. No matter what I do I can't get
> the warning to go away. Any ideas?

Brandon McCombs wrote:
> Another thing I ran into is something similar but with Hashtables. I get
> Type safety: The method put(Object, Object) belongs to the raw type
> Hashtable. References to generic type Hashtable<K,V> should be
> parameterized
>
> Is the fix similar for this as it was for the Vector?

John C. Bollinger wrote:
> Brandon McCombs wrote:
>> allAttribsAndVals.get(i).add( newField.getText() );
>>
>> allAttribsAndVals is declared as Vector<Vector> and the Vectors that
>> it holds are declared as Vector<String>. No matter what I do I can't
>> get the warning to go away. Any ideas?
>
> Declare allAttribsAndVals as as type Vector<Vector<String>>.
>

Thanks John. I didn't know I could nest them like that. It worked just
fine after that.

Brandon: cover your ears. What follows is likely to confuse you more
than it helps you.

Rhino wrote:
> The basic idea of generics is to introduce several features that encourage
> type safety in your code. Type safety has been present in languages like C++
> for years but were largely missing from Java until 1.5. I can't explain
> generics very well

[...]

I'm afraid I have to agree with you on that last part.

Generics are not *about* type safety. They are *about* extending the
Java type system with parameterized types. Use of such types can
greatly reduce the need to perform type conversions that cannot be
proved valid by the compiler; this is what most people mean when they
talk about "type safety" with respect to Generics. Type safety in that
sense is an important application of parameterized types, but not the
central idea.

C++ has had parameterized types ("templates") for a long time, but its
type system is overall much weaker than Java's, thus, in a broader sense
of the term, it has poorer type safety than Java does now or did before
the introduction of generics.

John C. Bollinger schrieb:
> Generics are not *about* type safety. They are *about* extending the
> Java type system with parameterized types. Use of such types can
> greatly reduce the need to perform type conversions that cannot be
> proved valid by the compiler; this is what most people mean when they
> talk about "type safety" with respect to Generics. Type safety in that
> sense is an important application of parameterized types, but not the
> central idea.

Java generics can only be used for type safety because the type info is
erased. What else can you use java generics for?

On Mon, 13 Mar 2006 13:13:18 +0100, Timo Stamm <>
wrote, quoted or indirectly quoted someone who said :
>Java generics can only be used for type safety because the type info is
>erased. What else can you use java generics for?

I think it would be more accurate to say that Java Generics can be
used to check type consistency EVEN THOUGH the type information is
erased before run time. Generics are a purely compile time check.

They also help the compiler to generate more efficient code. Generics
give the code generator hints when it can presume something more
specific than Objects being tossed about.
--
Canadian Mind Products, Roedy Green.http://mindprod.com Java custom programming, consulting and coaching.

Since the type information is 'compiled out', I can't see how Generics
would allow the compiler to generate more efficient code. I've reverse
compiled some Generic constructs and they look pretty much identical to
their non-Generic cousins.

"jlowery05" <> wrote in news:1142288566.230274.163740
@i39g2000cwa.googlegroups.com:
> Since the type information is 'compiled out', I can't see how Generics
> would allow the compiler to generate more efficient code. I've reverse
> compiled some Generic constructs and they look pretty much identical to
> their non-Generic cousins.

My understanding is that without Generics, the compiler sometimes must insert
casts and type checks to perform verification at runtime.
With Generics, some of these casts and type checks can be eliminated because
the checking has been performed at compile time.

I am still learning Generics (there is a lot to learn), so I hope that I got
this right.

Ian Shef schrieb:
> "jlowery05" <> wrote in news:1142288566.230274.163740
> @i39g2000cwa.googlegroups.com:
>
>> Since the type information is 'compiled out', I can't see how Generics
>> would allow the compiler to generate more efficient code. I've reverse
>> compiled some Generic constructs and they look pretty much identical to
>> their non-Generic cousins.
>
> My understanding is that without Generics, the compiler sometimes must insert
> casts and type checks to perform verification at runtime.
> With Generics, some of these casts and type checks can be eliminated because
> the checking has been performed at compile time.

No, the information is erased at compile time. From the java generics
tutorial:

Timo Stamm <> wrote:
> Java generics can only be used for type safety because the type info is
> erased. What else can you use java generics for?

By far the most important use is to communicate to other developers
using an API the exactly type of object they need to use to interact
with your classes. Generic type parameters are described by JavaDoc,
recognized and used by "auto-complete" features of development
environments, etc. This really doesn't fit under "type safety" so much
as convenience. As John said, though, it is described as a benefit of
extending the type system.

I'm saying this is a local benefit. It should not be taken to imply
that I agree generics are a net benefit to the language. I think the
jury is still out on that one.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!