Java:向量声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11697366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: vector declaration
提问by wesley.ireland
When I type my declare statement:
当我输入我的声明语句时:
Vector<double> distance_vector = new Vector<double>();
I receive the error (underlining 'double' in both cases):
我收到错误(在两种情况下都强调“double”):
Syntax error on token "double", Dimensions expected after this token
标记“double”的语法错误,此标记后的预期尺寸
What am I doing wrong here?
我在这里做错了什么?
回答by Jeffrey
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 Trovecollections if you really need to avoid the performance hit of boxing/unboxing.
您不能使用原语作为类型参数。如果您确实需要避免装箱/拆箱的性能影响,您要么需要使用Vector<Double>
(甚至更好的, List<Double>
)或使用Trove集合之一。
回答by Areeha
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.
最好的方法是使用 Vector,因为此类将原始类型 double 的值包装在一个对象中,该对象包含一个类型为 double 的单个字段。此外,它还允许您使用字符串类型进行转换。
回答by Quick n Dirty
Java generics can only hold objects, not primitives
Java泛型只能保存对象,不能保存原语
Oh, too late ; )
哦,太晚了;)
回答by Werneck
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.
将“n”替换为您的向量将拥有的位置数。如果你愿意,你可以把它做得更大,我没有弄错。如果不希望向量的大小固定,则应使用 Array 或 ArrayList 而不是向量。