how to solve this warning in Eclipse?

Discussion in 'Java' started by Brandon McCombs, Mar 12, 2006.

  1. 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

    thanks
     
    Brandon McCombs, Mar 12, 2006
    #1
    1. Advertising

  2. Brandon McCombs

    Rhino Guest

    "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
     
    Rhino, Mar 13, 2006
    #2
    1. Advertising

  3. 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?
     
    Brandon McCombs, Mar 13, 2006
    #3
  4. 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, Mar 13, 2006
    #4
  5. Brandon McCombs

    Rhino Guest

    "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:

    HashTable<Integer, String> myHashTable = new HashTable<Integer, String>();

    --
    Rhino
     
    Rhino, Mar 13, 2006
    #5
  6. Brandon McCombs

    Rhino Guest

    "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 at
    http://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.

    --
    Rhino
     
    Rhino, Mar 13, 2006
    #6
  7. 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>>.

    --
    John Bollinger
     
    John C. Bollinger, Mar 13, 2006
    #7
  8. 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?


    Yes.

    You might want to read
    http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html for a
    brief introduction to generics. There is also a link there to a
    lengthier, tutorial-style writeup on generics that you may find useful.

    --
    John Bollinger
     
    John C. Bollinger, Mar 13, 2006
    #8
  9. 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 McCombs, Mar 13, 2006
    #9
  10. 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 Bollinger
     
    John C. Bollinger, Mar 13, 2006
    #10
  11. Brandon McCombs

    Roedy Green Guest

    Roedy Green, Mar 13, 2006
    #11
  12. Brandon McCombs

    Timo Stamm Guest

    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?


    Timo
     
    Timo Stamm, Mar 13, 2006
    #12
  13. Brandon McCombs

    Roedy Green Guest

    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.
     
    Roedy Green, Mar 13, 2006
    #13
  14. Brandon McCombs

    jlowery05 Guest

    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, Mar 13, 2006
    #14
  15. Brandon McCombs

    Ian Shef Guest

    "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 805/F6 * These are my personal opinions
    Raytheon Company * and not those of my employer.
    PO Box 11337 *
    Tucson, AZ 85734-1337 *
     
    Ian Shef, Mar 16, 2006
    #15
  16. Brandon McCombs

    Timo Stamm Guest

    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:

    | Basically, erasure gets rid of (or erases) all generic type
    | information. All the type information betweeen angle brackets is
    | thrown out, so, for example, a parameterized type like List<String> is
    | converted into List. All remaining uses of type variables are replaced
    | by the upper bound of the type variable (usually Object). And,
    | whenever the resulting code isn’t type-correct, a cast to the
    | appropriate type is inserted [...]



    Timo
     
    Timo Stamm, Mar 16, 2006
    #16
  17. Brandon McCombs

    Chris Smith Guest

    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.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Mar 26, 2006
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chao
    Replies:
    6
    Views:
    1,311
    Mike Treseler
    Jul 5, 2004
  2. Cram TeXeD
    Replies:
    0
    Views:
    372
    Cram TeXeD
    Jun 12, 2004
  3. Pete Becker
    Replies:
    0
    Views:
    1,406
    Pete Becker
    Feb 10, 2005
  4. Ross

    weird 'C' warning - can you solve this?

    Ross, Feb 24, 2004, in forum: C Programming
    Replies:
    11
    Views:
    723
  5. princess.d
    Replies:
    1
    Views:
    459
    princess.d
    Jul 8, 2008
Loading...

Share This Page