4

I'm not really sure what is wrong with my code...

    public Vector blob (Pixel px)
{
    Vector v = new Vector();

    Point p = new Point(px.getX(), px.getY());
    v.add(p);

    return v;
}

I get the following: warning: [unchecked] unchecked call to add(E) as a member of the raw type Vector

v.add(p);

where E is a type-variable: E extends Object declared in class Vector.

Looking through the API, the add function takes an object as a param which I clearly make the line before, ideas?

wesley.ireland
  • 643
  • 3
  • 12
  • 21
  • 1
    Slightly off-topic: while you are at it, you might consider replacing `Vector` with `ArrayList` and use the synchronization package to get a synchronized List if you really need it. Vector is a bit of an odd animal, still non-deprecated because of legacy code – Miquel Jul 16 '12 at 15:56
  • Read this link based on Miquel's comment: [Why is Java Vector class considered obsolete or deprecated?](http://stackoverflow.com/a/1386288/1065197) – Luiggi Mendoza Jul 16 '12 at 16:00

4 Answers4

6

Starting with Java-5, java.util.Vector<E> is a generic container, meaning (in very plain terms) that you can specify the type of the element that you are planing to store, and have the compiler check that type for you automatically. Using it without the type of the element is OK, but it triggers a warning.

This should get rid of the warning, and provide additional type safety checks:

public Vector<Point> blob (Pixel px) {
    Vector<Point> v = new Vector<Point>();
    Point p = new Point(px.getX(), px.getY());
    v.add(p);
}

Another thing to note about vectors is that they are synchronized containers. If you are not planning to use the fact that they are synchronized, you may be better off with ArrayList<E> containers.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
5

Generics!!!. You have to specify the Object type for the contents of the vector. To remove the warning the vector code should be like this

Vector<Point> v=new Vector<Point>()
Akhi
  • 2,242
  • 18
  • 24
3

It is because generics. Generics allows type safety where compiler checks the type at compile time. When you are using raw types for generic parameter class/method, you will get this warning.

Raw types are the ones which doesn't provide concrete implementation for the generic parameter.

In this case add method for vector class is "generic" which allows to tell the concrete implementation type add method accepts.

To allow backward compatibility, java still allows you to define "raw types". Read this documentation for more information.

kosa
  • 65,990
  • 13
  • 130
  • 167
0

You're forgetting to use Java Generics.

Short answer: Declare Vector<Pixel > v = new Vector<Pixel >() instead of
Vector v = new Vector()

Long answer: Read about generics and understand its usage

Also, you might want to reconsider your usage of Vectors. Here's a SO link that explains why.

Community
  • 1
  • 1
Sujay
  • 6,753
  • 2
  • 30
  • 49