0

I am getting this error but don't know how to resolve it.I have already gone through the post but still confused. I tried to run Xlint File.java but its not working. While running this code, I am getting the msg like

"VectorTest.java uses unchecked or unsafe operation

Recompile with -Xlint:unchecked for details"

import java.util.Vector;
import java.util.Enumeration;

public class VectorTest
{
  public static void main(String args[])
  {

       Vector v=new Vector(3,4);
       System.out.println("Initial size"+ v.size());
       System.out.println("Initial capacity"+ v.capacity());
       v.addElement(new Integer(1));
       v.addElement(new Float(2.5));
       v.addElement(new Double(3.45));
       v.addElement(new Integer(4));
       v.addElement(new Integer(5));
       System.out.println("Current Capacity"+ v.capacity());

       v.addElement(new Integer(6));
       v.addElement(new Integer(7));
       v.addElement(new Integer(8));
       v.addElement(new Integer(9));
       v.addElement(new Integer(10));
       v.addElement(new Integer(11));
       v.addElement(new Integer(12));
       System.out.println("Current Capacity"+ v.capacity());
       System.out.println("First element"+(Integer) v.firstElement());
       System.out.println("Last element"+(Integer)v.lastElement());
       Enumeration e;
       e= v.elements();
       while(e.hasMoreElements())
       {
          System.out.println("All elements"+ e.nextElement());
       }
    }
 }
vidit
  • 6,293
  • 3
  • 32
  • 50
Abhinay
  • 1,796
  • 4
  • 28
  • 52

2 Answers2

2

The Vector class is generic; you should specify the type of objects it contains. In your case, it looks like you should declare it to contain either Object or Number (both of which are superclasses of everything you seem to be adding):

Vector<Number> v = new Vector<>(3,4); // or new Vector<Number>(3,4) if pre-Java 7

The warning should then go away.

As an aside, it is better to use the construct Integer.valueOf(1), etc. instead of new Integer(1). It allows re-use of Integer objects that may be cached, cutting down on memory use.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • +1 for explanation, using Java 7 and the `valueOf` best practice – robjohncox Jul 03 '13 at 06:27
  • sorry but i am not able to grab the concept of Vector=new Vector<>(3,4); What is the use of here, can you please explain – Abhinay Jul 03 '13 at 15:16
  • @user2545197 - `Number` (not `number`) is the base class for `Integer`, `Float`, etc. The notation `` is how one specifies a type parameter when using a generic class. `Vector` tells the compiler that this particular `Vector` will only hold instances of `Number`. Generics provide a way of improving type safety at compile time, as well as greatly reducing the need to cast objects to specific types. See the [Java tutorial on generics](http://docs.oracle.com/javase/tutorial/java/generics/) for more information about this. – Ted Hopp Jul 03 '13 at 15:34
  • Thanks, but pls may i know that if i place in Vector, in this way am i going to restrict my vector elements to be only a Number type of values..correct me if i am asking the wrong question..thanks in advance. – Abhinay Jul 03 '13 at 16:22
  • @user2545197 - If you want no restriction on what you place in a `Vector`, then you can always use `` as your type parameter. That places no restrictions, since anything that goes into a `Vector` must be an `Object` (the collection classes cannot contain primitive values). You sacrifice all type checking at compile time, but if that's what you need.... Not that a bare `Vector` and `Vector` are functionally (and semantically) identical; the difference is that in the second case you've explicitly told the compiler what you want, so it won't issue any warnings. – Ted Hopp Jul 03 '13 at 17:06
  • thats really grt..it solves all my problems..A Big Thanks to you – Abhinay Jul 04 '13 at 05:46
1

You're missing the generics types. You could declare your Vector as

Vector<Number> v = new Vector<Number>(3, 4);
Kayaman
  • 72,141
  • 5
  • 83
  • 121