Java 1.5 generics warning messages

R

RC

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!!
 
I

Ingo R. Homann

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
 
T

Tjerk Wolterink

RC said:
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
 
D

Dale King

Tjerk said:
RC said:
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.
 
T

Tjerk Wolterink

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top