-1

The following is an example of an error that is incorrectly specified. How should I fix it and why did I get an error?

Vector <int> vi = new Vector <int> ();

Vuslat
  • 33
  • 8
  • 1
    You can not use primitive types in Generics. int primitive data type while Integer is a Wrapper class, so you have to use Vector vi = new Vector (); – A Paul Dec 25 '20 at 18:04
  • 2
    *why did I get an error?* - what error? Whenever you ask a question be specific and post the exact error message so we don't have to guess what you are asking. – camickr Dec 25 '20 at 18:04
  • As a side note: https://stackoverflow.com/questions/629804/what-is-the-most-efficient-java-collections-library – terrorrussia-keeps-killing Dec 25 '20 at 19:21

1 Answers1

2

Java generics don't support primitive types like int. You can use the java.lang.Integer wrapper class instead:

Vector <Integer> vi = new Vector<Integer>();

As a side note, while Vector isn't officially deprecated, it's been considered outdated since JDK 1.2, and you should probably use an ArrayList instead.

Mureinik
  • 297,002
  • 52
  • 306
  • 350