I have a medium-sized Java project in Eclipse, which uses Vector<E> instead of the preferred ArrayList<E>. I would like to replace those.
-
Is there any way, using Eclipse, to change all these? Any refactoring method?
-
Or would it be sufficient to do a general search-and-replace for every
Vector<String>occurrence? Are there any caveats to this? What are situations in which this approach would fail?
Actually, I just did the latter, and it worked for my application, but this should be a more general question.
Vectorwas retrofitted to implementListin Java 1.2 when the Collections API, which includesArrayList, was introduced. So it has both old-style methods likeelementAt(), and new-style methods likeget(), which are largely work-alike.The old methods aren’t in
ListorArrayList, so if you searched and replaced, and were using old methods, it would fail to compile. Easy enough to find and fix those, though. Same foriterator()/IteratorreplacingEnumerationand such.Vector‘s operations were synchronized; if the program relied on that for correctness, it could fail if replaced withArrayList, which is not. Wrap withCollections.synchronizedList()if you need the old behavior. This is a source of subtler bugs.