Java 1.5 generics warning messages

Discussion in 'Java' started by RC, Jun 29, 2005.

  1. RC

    RC Guest

    Since I upgrade Java from 1.4.2 to 1.5.002, each time
    I got those generics warning messages.
    Please help me get rid those messages.

    public class MyClass {
    private Vector vector;

    public MyClass(Vector vector) { this.vector = vector; }

    public Vector getVector() { return vector; }
    public void addVector(Object object) { vector.addElement(object); }
    }

    .....

    MyClass dude = new MyClass(new Vector<String>());

    dude.addVector("The Hell");
    // Here I got warning from MyClass addVector method

    warning: [unchecked] unchecked call to addElement(E) as a member of the
    raw type java.util.Vector
    vector.addElement(object);

    Vector<String> vv = dude.getVector();
    // Here I also get warning message

    warning: [unchecked] unchecked cast
    found : java.util.Vector
    required: java.util.Vector<String>
    Vector<String> vv = (Vector<String>)dude.getVector();

    Can someone out there help me get rid those warning messages?
    Thank Q very much in advance!!
     
    RC, Jun 29, 2005
    #1
    1. Advertisements

  2. Hi RC,

    you are mixing generics and non-generic code. Try the following:

    public class MyClass<T> {
    private Vector<T> vector;

    public MyClass(Vector<T> vector) { this.vector = vector; }

    public Vector<T> getVector() { return vector; }
    public void addVector(T object) { vector.addElement(object); }
    }

    Ciao,
    Ingo
     
    Ingo R. Homann, Jun 29, 2005
    #2
    1. Advertisements

  3. RC wrote:
    > Since I upgrade Java from 1.4.2 to 1.5.002, each time
    > I got those generics warning messages.
    > Please help me get rid those messages.
    >
    > public class MyClass {
    > private Vector vector;
    >
    > public MyClass(Vector vector) { this.vector = vector; }
    >
    > public Vector getVector() { return vector; }
    > public void addVector(Object object) { vector.addElement(object); }
    > }
    >
    > ....
    >
    > MyClass dude = new MyClass(new Vector<String>());
    >
    > dude.addVector("The Hell");
    > // Here I got warning from MyClass addVector method
    >
    > warning: [unchecked] unchecked call to addElement(E) as a member of the
    > raw type java.util.Vector
    > vector.addElement(object);
    >
    > Vector<String> vv = dude.getVector();
    > // Here I also get warning message
    >
    > warning: [unchecked] unchecked cast
    > found : java.util.Vector
    > required: java.util.Vector<String>
    > Vector<String> vv = (Vector<String>)dude.getVector();
    >
    > Can someone out there help me get rid those warning messages?
    > Thank Q very much in advance!!
    >
    >


    You can just ignore those warning message if you want your app to be compatible
    with java 1.4
     
    Tjerk Wolterink, Jun 29, 2005
    #3
  4. RC

    Dale King Guest

    Tjerk Wolterink wrote:
    > RC wrote:
    >
    >> Since I upgrade Java from 1.4.2 to 1.5.002, each time
    >> I got those generics warning messages.
    >> Please help me get rid those messages.
    >>
    >> public class MyClass {
    >> private Vector vector;
    >>
    >> public MyClass(Vector vector) { this.vector = vector; }
    >>
    >> public Vector getVector() { return vector; }
    >> public void addVector(Object object) { vector.addElement(object); }
    >> }
    >>
    >> ....
    >>
    >> MyClass dude = new MyClass(new Vector<String>());
    >>
    >> dude.addVector("The Hell");
    >> // Here I got warning from MyClass addVector method
    >>
    >> warning: [unchecked] unchecked call to addElement(E) as a member of
    >> the raw type java.util.Vector
    >> vector.addElement(object);
    >>
    >> Vector<String> vv = dude.getVector();
    >> // Here I also get warning message
    >>
    >> warning: [unchecked] unchecked cast
    >> found : java.util.Vector
    >> required: java.util.Vector<String>
    >> Vector<String> vv = (Vector<String>)dude.getVector();
    >>
    >> Can someone out there help me get rid those warning messages?
    >> Thank Q very much in advance!!
    >>
    >>

    >
    > You can just ignore those warning message if you want your app to be
    > compatible
    > with java 1.4


    That's not really an issue in this case, since the OP is actually using
    generics so he will not be compatible with 1.4. The issue is that he is
    mixing generics with non-generics.

    --
    Dale King
     
    Dale King, Jun 30, 2005
    #4

  5. >
    > That's not really an issue in this case, since the OP is actually using
    > generics so he will not be compatible with 1.4. The issue is that he is
    > mixing generics with non-generics.
    >


    ow my fault.
     
    Tjerk Wolterink, Jun 30, 2005
    #5
    1. Advertisements

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. Thomas G. Marshall

    1.5: generics warning of using Raw datatype

    Thomas G. Marshall, Aug 17, 2004, in forum: Java
    Replies:
    7
    Views:
    22,005
    knightpraetor
    Jun 11, 2011
  2. Juergen Berchtel
    Replies:
    1
    Views:
    6,539
    John C. Bollinger
    May 20, 2005
  3. Roedy Green

    Generics error messages

    Roedy Green, Jul 31, 2005, in forum: Java
    Replies:
    22
    Views:
    1,165
    Roedy Green
    Aug 5, 2005
  4. Nicolai Henriksen

    Old code, collections and generics warning

    Nicolai Henriksen, Nov 8, 2005, in forum: Java
    Replies:
    14
    Views:
    889
    Nicolai Henriksen
    Nov 11, 2005
  5. Christian Pontesegger

    Generics compiler warning

    Christian Pontesegger, Apr 27, 2006, in forum: Java
    Replies:
    3
    Views:
    6,695
    Christian Pontesegger
    May 2, 2006
  6. Thomas Hawtin

    Q: Warning with generics

    Thomas Hawtin, Jul 31, 2006, in forum: Java
    Replies:
    2
    Views:
    414
    G. Ralph Kuntz, MD
    Jul 31, 2006
  7. Roedy Green

    Generics warning message.

    Roedy Green, Dec 30, 2007, in forum: Java
    Replies:
    4
    Views:
    505
  8. Soul
    Replies:
    0
    Views:
    730
Loading...