Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of std::vector in Java?

Tags:

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

like image 897
jmasterx Avatar asked Sep 16 '10 21:09

jmasterx


People also ask

What is equivalent of Vector in Java?

The java. util. vector. equals(Object obj) method of Vector class in Java is used verify the equality of an Object with a vector and compare them.

What is Vector in c++ similar to in Java?

“c++ vector push_back equivalent in java” Code Answer // pop_back equivalent.

Does Java have a Vector?

Vector implements a dynamic array which means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index.

Is Vector outdated in Java?

Vector class is often considered as obsolete or “Due for Deprecation” by many experienced Java developers. They always recommend and advise not to use Vector class in your code. They prefer using ArrayList over Vector class.


2 Answers

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

like image 133
Nikita Rybak Avatar answered Sep 29 '22 13:09

Nikita Rybak


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

Do not use the Stack class as other here suggest.

like image 27
helpermethod Avatar answered Sep 29 '22 13:09

helpermethod