Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change from ArrayList to Vector

I'm working on an android game and I just noticed that since onTouchEvent runs on the UI thread, and the update/render methods are ran from a separate threads, both of them update an ArrayList which contains the entities. So obviously they conflict if they happen to modify the list at the same time.

I read that Vector class is used exactly the same as ArrayList with the only difference that Vector is synchronized, ergo they wont conflict. Is that true? if so, does it have any performance issue or something that I should be concerned about? I have never used Vector class before.

EDIT: what I actually meant was change from

ArrayList<Obj> list = new ArrayList<Obj>();

to

Vector<Obj> list = new Vector<Obj>()

But as the answers say, Vector is not recommended to use. The selected answer solved my issue.

like image 214
Christopher Francisco Avatar asked Jul 04 '13 21:07

Christopher Francisco


People also ask

Which is better ArrayList or Vector?

Performance: ArrayList is faster. Since it is non-synchronized, while vector operations give slower performance since they are synchronized (thread-safe), if one thread works on a vector, it has acquired a lock on it, which forces any other thread wanting to work on it to have to wait until the lock is released.

Can ArrayList convert into array?

ArrayLists are resizable arrays and can store elements of type wrapper class objects. Java provides the flexibility of converting ArrayLists to Array and vice versa. 3 ways of conversion - manual conversion using get() method, using Object[] toArray() method, using T[] toArray(T[] arr) method.

Is Vector deprecated in Java?

They are obsolete, but they are not deprecated.


2 Answers

For those who have to fight with legacy code do just the following:

new Vector<Obj>(anyThingWhichImplemntsCollection);
like image 196
Sergio Gabari Avatar answered Sep 18 '22 13:09

Sergio Gabari


It's oldie Vector try to not use Vector instead use

synchronizedList

Example :

list = Collections.synchronizedList(list);

Vector is considered obsolete and deprecated read Why vector is considerer obsolete?

like image 27
nachokk Avatar answered Sep 19 '22 13:09

nachokk