相当于 Java 中的 std::vector?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3731067/
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
Equivalent of std::vector in Java?
提问by jmasterx
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).
Java 中最接近 std::vector 的是什么?我的意思是,一个类可以将 T 引入其构造函数,然后 pushBack、popBack() 并存储在连续内存中(不是链表)。
Thanks
谢谢
采纳答案by Nikita Rybak
ArrayList
Everything's stored in array ("continuous memory") internally, although operation names are a bit different.
ArrayList
一切都在内部存储在数组(“连续内存”)中,尽管操作名称有点不同。
A bit more about list implementations in Java
And about generics
edit
Helper Methodalso mentioned useful class in his answer (although not exactly equivalent to C++ Vector).
编辑
Helper Method在他的回答中也提到了有用的类(虽然不完全等同于 C++ Vector)。
回答by Jaime Garcia
Java has Stack which supports push and pop. (http://download.oracle.com/javase/6/docs/api/java/util/Stack.html)
Java 具有支持推送和弹出的堆栈。( http://download.oracle.com/javase/6/docs/api/java/util/Stack.html)
回答by Yevgeny Simkin
Is ArrayList what you're looking for? ArrayList l = new ArrayList<String>();
So you can have a list of anything (defined between the <>).
ArrayList 是您要查找的内容吗?ArrayList l = new ArrayList<String>();
因此,您可以拥有任何内容的列表(在 <> 之间定义)。
回答by helpermethod
That would probably be ArrayDeque, if you need Stack functionality.
如果您需要 Stack 功能,那可能是ArrayDeque。
Do not use the Stackclass as other here suggest.
不要像这里建议的那样使用Stack类。
回答by Colin Hebert
You can use an ArrayDeque
, it doesn't support random access but support Deque
(double ended queue) methods
您可以使用ArrayDeque
,它不支持随机访问但支持Deque
(双端队列)方法
回答by The Mighty Rubber Duck
How about simply the Vector class?
简单的 Vector 类怎么样?
http://download-llnw.oracle.com/javase/6/docs/api/java/util/Vector.html
http://download-llnw.oracle.com/javase/6/docs/api/java/util/Vector.html
回答by Eamon Nerbonne
You're probably looking for the ArrayDeque
which supports push/pop style access from both ends of the list efficiently.
您可能正在寻找有效ArrayDeque
支持从列表两端推送/弹出样式访问的 。
AvoidStack
and Vector
- these are synchronized, which implies generally pointless overhead.
避免Stack
和Vector
- 这些是同步的,这意味着通常无意义的开销。
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.
ArrayList
也不错;但是,您需要实现自己的(简单的)pop 方法,因为它不是由类本身提供的。 ArrayList
确实允许索引访问,这是ArrayDeque
缺乏的。
回答by cabumtz
What you need is exactly an java.util.ArrayList<T>
You can check the documentation in http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
您需要的正是一个java.util.ArrayList<T>
您可以查看http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 中的文档
Basically is a List implemented with an Array where the references live in a continuous chunk of memory.
基本上是一个用数组实现的列表,其中引用存在于连续的内存块中。
I recommend to use in combination with a interface variable like this: List<String> stringList = new ArrayList<String>();
so if you decide, you can change the implementation to java.util.LinkedList<T>
or another one.
我建议与这样的接口变量结合使用:List<String> stringList = new ArrayList<String>();
因此,如果您决定,可以将实现更改为java.util.LinkedList<T>
或 另一个。
回答by elghareb
i think it is the LinkedList
我认为它是 LinkedList
vector (c++) <===========> linkedlist(java)
v.front() <===========> l.peekFirst()
v.back() <===========> l.peekLast()
v.push_back(x) <===========> l.add(x)
v.pop_back() <===========> l.pollLast()