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
i think it is the 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()
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.
You can use an ArrayDeque, it doesn't support random access but support Deque
(double ended queue) methods
Java has Stack which supports push and pop. (http://download.oracle.com/javase/6/docs/api/java/util/Stack.html)
How about simply the Vector class?
http://download-llnw.oracle.com/javase/6/docs/api/java/util/Vector.html
That would probably be ArrayDeque, if you need Stack functionality.
Do not use the Stack class as other here suggest.