Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java vector warnings

Tags:

java

vector

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?

like image 594
wesley.ireland Avatar asked Dec 08 '22 22:12

wesley.ireland


2 Answers

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.

like image 137
Sergey Kalinichenko Avatar answered Dec 30 '22 23:12

Sergey Kalinichenko


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>()
like image 44
Akhi Avatar answered Dec 30 '22 22:12

Akhi