When is it better to use a vector than an array, and vice versa in java?

When is it better to use a vector than an array, and vice versa in java? and why?

+5
source share
4 answers

Vector: never if the API does not require this, because it is a class, not an interface.

List: this should be your default collection of arrays. This is an interface, so anything can be List, if necessary. (and there are many implementations List, for example, ArrayList, LinkedList, CopyOnWriteArrayList, ImmutableListfor different sets of functions)

VectorIt is thread safe, but also a shell Collections.synchronizedList().

: , API. - , List<Integer>, Integer.

+7

A Vector ( List), , .

An array, , .

List Vector ( ) , . , .

arrays, . : API, .

+2

-, ArrayList - , Vector ( ).

, ( ).

0

List.

Vector, JDK ​​ Collections.

If there is a very performance-sensitive algorithm, a private member of the array may be useful. If you need to return its contents or pass them to a method, it is best to build an object around it, perhaps as simple as Arrays.asList(thePrivateArray). For a thread-safe list: Collections.synchronizedList(Arrays.asList(thePrivateArray)). To prevent the contents of the array from being modified, I usually use Collections.unmodifiableList(Arrays.asList(thePrivateArray)).

0
source

All Articles