47

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

Bert F
  • 85,407
  • 12
  • 106
  • 123
jmasterx
  • 52,639
  • 96
  • 311
  • 557

9 Answers9

52

ArrayList
Everything's stored in array ("contiguous 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).

Community
  • 1
  • 1
Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
10

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

Do not use the Stack class as other here suggest.

helpermethod
  • 59,493
  • 71
  • 188
  • 276
  • 2
    Good call, though `ArrayList` would still be a closer match because it supports indexed element access like the C++ `vector`. – casablanca Sep 16 '10 at 21:52
5

Is ArrayList what you're looking for?
ArrayList l = new ArrayList<String>();
So you can have a list of anything (defined between the <>).

corazza
  • 31,222
  • 37
  • 115
  • 186
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
2

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.

Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166
1

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

Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
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

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.

cabumtz
  • 51
  • 1
  • 2
0

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

Jaime Garcia
  • 6,744
  • 7
  • 49
  • 61
0

How about simply the Vector class?

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

The Mighty Rubber Duck
  • 4,388
  • 5
  • 28
  • 27
0

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() 
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
elghareb
  • 25
  • 2
  • Aren't these completely different data types? – Troyseph Jun 15 '21 at 14:03
  • No. Linked list is when each element contains a pointer to the next and the previous element, these elements aren't store continuously in memory. Elements in vector ARE actually stored continuously, one after the other in memory, much like an array. – Dave F. Feb 08 '22 at 22:07
  • I believe this one is the best. The vector often declared empty, so contiguous allocation in memory doesn't matter if you don't need fast indexed access and LinkedList has all the equivalent methods available, as well as indexed access too(yes, not so fast). ArrayDeck doesn't have indexed access at all. – WebComer Mar 13 '22 at 21:09
  • No, LinkedList is the equivalent of std::list. The expectation of std::vector is that accessing by index is O(1) and LinkedList (and std::list) is O(n). Even if all you need is a place to store items and iterate over them, an array will take up less memory (no need for back/forward pointers) and have better cache performance. LinkedList is better if you are frequently adding and removing from the middle (O(1)), as an array would copy (O(n)). But OP specifically said NOT a linked list. – prewett Apr 27 '22 at 19:05