Java中std :: vector的等价物?

时间:2022-06-04 08:43:31

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

9 个解决方案

#1


33  

ArrayList
Everything's stored in array ("continuous memory") internally, although operation names are a bit different.

ArrayList Everything存储在数组(“连续内存”)内部,虽然操作名称有点不同。

A bit more about list implementations in Java
And about generics

更多关于Java中的列表实现和关于泛型的更多信息

edit
Helper Method also mentioned useful class in his answer (although not exactly equivalent to C++ Vector).

编辑助手方法在他的回答中也提到了有用的类(虽然不完全等同于C ++ Vector)。

#2


7  

That would probably be ArrayDeque, if you need Stack functionality.

如果你需要Stack功能,那可能就是ArrayDeque。

Do not use the Stack class as other here suggest.

不要像其他人建议的那样使用Stack类。

#3


2  

You're probably looking for the ArrayDeque which supports push/pop style access from both ends of the list efficiently.

您可能正在寻找有效支持列表两端的推/弹样式访问的ArrayDeque。

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.

ArrayList也没关系;但是,您需要实现自己的(普通)pop方法,因为它本身不提供它。 ArrayList允许ArrayDeque缺少的索引访问。

#4


2  

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 ();所以你可以有一个任何列表(在<>之间定义)。

#5


1  

You can use an ArrayDeque, it doesn't support random access but support Deque (double ended queue) methods

您可以使用ArrayDeque,它不支持随机访问,但支持Deque(双端队列)方法

#6


1  

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() 

#7


0  

Java has Stack which supports push and pop. (http://download.oracle.com/javase/6/docs/api/java/util/Stack.html)

Java有支持push和pop的Stack。 (http://download.oracle.com/javase/6/docs/api/java/util/Stack.html)

#8


0  

How about simply the Vector class?

简单地说Vector类怎么样?

http://download-llnw.oracle.com/javase/6/docs/api/java/util/Vector.html

#9


0  

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 你可以查看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.

基本上是一个使用Array实现的List,其中引用存在于连续的内存块中。

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 stringList = new ArrayList ();因此,如果您决定,可以将实现更改为java.util.LinkedList 或其他。

#1


33  

ArrayList
Everything's stored in array ("continuous memory") internally, although operation names are a bit different.

ArrayList Everything存储在数组(“连续内存”)内部,虽然操作名称有点不同。

A bit more about list implementations in Java
And about generics

更多关于Java中的列表实现和关于泛型的更多信息

edit
Helper Method also mentioned useful class in his answer (although not exactly equivalent to C++ Vector).

编辑助手方法在他的回答中也提到了有用的类(虽然不完全等同于C ++ Vector)。

#2


7  

That would probably be ArrayDeque, if you need Stack functionality.

如果你需要Stack功能,那可能就是ArrayDeque。

Do not use the Stack class as other here suggest.

不要像其他人建议的那样使用Stack类。

#3


2  

You're probably looking for the ArrayDeque which supports push/pop style access from both ends of the list efficiently.

您可能正在寻找有效支持列表两端的推/弹样式访问的ArrayDeque。

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.

ArrayList也没关系;但是,您需要实现自己的(普通)pop方法,因为它本身不提供它。 ArrayList允许ArrayDeque缺少的索引访问。

#4


2  

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 ();所以你可以有一个任何列表(在<>之间定义)。

#5


1  

You can use an ArrayDeque, it doesn't support random access but support Deque (double ended queue) methods

您可以使用ArrayDeque,它不支持随机访问,但支持Deque(双端队列)方法

#6


1  

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() 

#7


0  

Java has Stack which supports push and pop. (http://download.oracle.com/javase/6/docs/api/java/util/Stack.html)

Java有支持push和pop的Stack。 (http://download.oracle.com/javase/6/docs/api/java/util/Stack.html)

#8


0  

How about simply the Vector class?

简单地说Vector类怎么样?

http://download-llnw.oracle.com/javase/6/docs/api/java/util/Vector.html

#9


0  

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 你可以查看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.

基本上是一个使用Array实现的List,其中引用存在于连续的内存块中。

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 stringList = new ArrayList ();因此,如果您决定,可以将实现更改为java.util.LinkedList 或其他。