用于基元的 Java Vector 或 ArrayList
Java API 中是否有相当于 Vector 或 ArrayList 类的可扩展数组类,可以与基元(int、char、double 等)一起使用?
我需要一个快速、可扩展的整数数组,为了将它们与 Vector
或 ArrayList
一起使用,必须将它们包装在 Integer
类中似乎很浪费代码>. 我的 google-fu 让我失望了。
Is there an expandable array class in the Java API equivalent to the Vector
or ArrayList
class that can be used with primitives (int, char, double, etc)?
I need a quick, expandable array for integers and it seems wasteful to have to wrap them in the Integer
class in order to use them with Vector
or ArrayList
. My google-fu is failing me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不幸的是,没有这样的类,至少在 Java API 中是这样。 有 Primitive Collections for Java 第 3 方产品。
将自动装箱与现有集合类(特别是
List
实现)一起使用是非常危险的。 例如:它也是神秘的
NullPointerExceptions
访问的常见来源(可能通过Map
):There is unfortunately no such class, at least in the Java API. There is the Primitive Collections for Java 3rd-party product.
It's pretty dangerous to use auto-boxing together with existing collection classes (in particular
List
implementations). For example:And it is also a common source of mysterious
NullPointerExceptions
accessing (perhaps via aMap
):http://trove4j.sourceforge.net/
请注意,由于 Trove 使用基元,因此它定义的类型不实现 java.util 集合接口。
(LGPL 许可证)
http://trove4j.sourceforge.net/
Note that because Trove uses primitives, the types it defines do not implement the java.util collections interfaces.
(LGPL license)
现代Java支持原语的自动装箱,所以你可以说
这至少避免了new Integer(42)的语法醋。
Modern Java supports autoboxing of primitives, so you can say
That at least avoids the syntactic vinegar of new Integer(42).
Joda 基元。
还有 Java 原始集合,但它有点过时了。
Joda-Primitives.
There is also Primitive Collections for Java but it's a bit out of date.
Eclipse Collections 具有所有原始类型的原始 ArrayList,以及原始 Sets、Bags、Stacks 和 Maps。 所有原始容器类型也有不可变版本。
注意:我是 Eclipse Collections 的提交者。
Eclipse Collections has primitive ArrayLists for all primitive types, as well as primitive Sets, Bags, Stacks and Maps. There are immutable versions of all of the primitive container types as well.
Note: I am a committer for Eclipse Collections.