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.
Vector
was retrofitted to implement List
in Java 1.2 when the Collections API, which includes ArrayList
, was introduced. So it has both old-style methods like elementAt()
, and new-style methods like get()
, which are largely work-alike.
The old methods aren't in List
or ArrayList
, 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 for iterator()
/Iterator
replacing Enumeration
and such.
Vector
's operations were synchronized; if the program relied on that for correctness, it could fail if replaced with ArrayList
, which is not. Wrap with Collections.synchronizedList()
if you need the old behavior. This is a source of subtler bugs.
The only things that could make it go wrong are
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With