2

When I type my declare statement:

Vector<double> distance_vector = new Vector<double>();

I receive the error (underlining 'double' in both cases):

Syntax error on token "double", Dimensions expected after this token

What am I doing wrong here?

wesley.ireland
  • 643
  • 3
  • 12
  • 21

5 Answers5

8

You cannot use primitives as type parameters. You either need to use a Vector<Double> (or even better, List<Double>) or use one of the Trove collections if you really need to avoid the performance hit of boxing/unboxing.

Community
  • 1
  • 1
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • 1
    The "performance hit" is a great deal less than you might think. Certainly not enough to warrant using a special library, less you are trying to squeeze the very last drop of performance out of some code. Even then, I'd need to see benchmarks that prove it was worth it. – Bohemian Jul 28 '12 at 01:19
  • @Bohemian I agree. But there are special situations where that performance hit is great enough to bring your program to a crawl, in which case that special library is necessary. And since the OP didn't mention his need, I added it in for completeness. – Jeffrey Jul 28 '12 at 01:21
1

Java generics can only hold objects, not primitives

Oh, too late ; )

Quick n Dirty
  • 569
  • 2
  • 7
  • 15
1

The best approach is to use Vector as this class wraps a value of the primitive type double in an object which contains a single field whose type is double. Also, it allows you to convert with string type.

Areeha
  • 823
  • 7
  • 11
0

You should go with:

double [n] vector;

Replace "n" for the number of positions your vector will have. You can make it bigger, if you want and I'm not mistaken. If you want the size of your vector not to be fixed, you should use an Array or ArrayList instead of a vector.

HWerneck
  • 89
  • 1
  • 10
0

Use this:

Vector < Double > distance_vector = new Vector < Double >();

It is working.

Dada
  • 6,313
  • 7
  • 24
  • 43