What are the differences between Vector and ArrayList? Which one is better?
- Arraylist is not synchronized while Vector is.
- Arraylist has no default size while Vector has a default size of 10. Both of them represent a "growable array".
- Arraylist don't define any increment size while Vector does.
- While the
iteratorthat are returned by both classes are fail-fast. TheEnumerationreturned by Vector are not.
ArrayList is part of the Java collection framework. You should use it in most time to take advantages of Java collection framework. You probably won't need synchronization in most cases. It is easy enough to create a synchronized ArrayList using the following code:
Collections.synchronizedList(new ArrayList());
Notice: From ArrayList API Document:
The iterators returned by this class's iterator and listIterator methods are fail-fast: if list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Most Recent java Faqs
- How to avoid an java.util.ConcurrentModificationException with ArrayList?
- How to convert a given array to a list in Java?
- How to make Java objects eligible for garbage collection?
- What are local variables in Java?
- What are instance variables in Java?
- How many backslashes?
- What are class variables in Java?
Most Viewed java Faqs
- How to use HttpURLConnection POST data to web server?
- What is runtime polymorphism in Java?
- How to add BASIC Authentication into HttpURLConnection?
- What is String literal pool?
- Can the run() method be called directly to start a thread?
- What does Class.forname method do?
- How to read input from console (keyboard) in Java?