Equivalent of std::vector in Java?
What would be the closest thing to a std::vector in Java? By this I mean, a class which can take in T into its constructor and then pushBack, popBack() and that is stored in continuous memory (not linked list).
Thanks
ArrayList
Everything's stored in array ("continuous memory") internally, although operation names are a bit different.
A bit more about list implementations in Java
And about generics
edit
Helper Method also mentioned useful class in his answer (although not exactly equivalent to C++ Vector).
That would probably be ArrayDeque, if you need Stack functionality.
Do not use the Stack class as other here suggest.
You're probably looking for the ArrayDeque
which supports push/pop style access from both ends of the list efficiently.
Avoid Stack
and Vector
- these are synchronized, which implies generally pointless overhead.
ArrayList
is also fine; however, you'd need to implement your own (trivial) pop method since it is not provided by the class itself. ArrayList
does permit indexed access, which ArrayDeque
lacks.
上一篇: Java:列表列表的笛卡尔积