There is one difference on how these colelction handle the iteration while the collection is still modifying by program.ĪrrayList provide iterators, which are fail-fast. ArrayList vs Vector – ConcurrentModificationException Whereas Vector is synchronized, so it has some overhead in thread management/ locking etc. ArrayList is non-synchronized so there is no time lapse in thread safety. Real performance difference comes when we take synchronization into consideration. So essentially there is not much performance difference in add and get operations. ArrayList vs Vector – Performanceīoth collections have a backing array on which they store and search elements. In arraylist, we can define the initial capacity but not the capacity increment factor. In ArrayList, by default capacity grows by 50% of existing capacity. Here second argument is the amount by which the capacity is increased when the vector overflows. We can overide the default capacity using constructor public Vector(int initialCapacity, int capacityIncrement). It means vector size grows to double of previous capacity. ArrayList vs Vector – Capacity incrementīy default when vector needs to increase capacity to add an element (when existing capacity is filled), it increases the capacity by 100%. List syncNamesList = Collections.synchronizedList(namesList) Ģ. But it have the same capability as arraylist class. Returned list is an internal implementation of List interface different from arraylist. If we want to get sunchronized version of arraylist, then we can use Collections.synchronizedList() method. To use arraylist in concurrent application, we must explicitely control the thread access to instance to make application work as intended. Public methods inside vector are defined synchronized which make all operations in vector safe for concurrency needs. It simply means that when working on concurrent applications, we can use Vector without any addtional synchronization control implemented by developer using synchronized keyword. Vector is a synchronized collection and ArrayList is not. Still they are different in many aspects and we need to understand both classes in detail to make a wise decision when to use which class. To get the size of Array we use the length() method but in ArrayList we use the size() method to find the size of ArrayList.ArrayList and Vector, both implements interface and provide capability to store and get objects within using simple API methods. We can make an Array as multi-dimensional but an ArrayList is always single dimensional. In Array we add the element by use assignment operator (=), But in ArrayList, we add the element by use of add() method. To iterate an ArrayList we can use for loop, forEach loop, Iterator, List Iterator. We can Iterate the Array by use of for loop, forEach loop. Here JVM converts the int to Integer type. Here ArrayList is can take only Integer type of data. Primitive and non-primitiveĪn Array can hold primitive(int, float, double) and non-primitive(String or User-defined class) data type.īut some developers misunderstood it and think ArrayList can take primitive data type also. It is mandatory to provide the size of the Array.Īn ArrayList is automatically initialized by default size 10. InitializationĪn Array should be initialized by some value. So ArrayList restrict the different type of data in generic ArrayList. By use of the generic concept, JVM ensures the type safety at compile time. But we should try to get errors at compile time.ĪrrayList supports the generic concept, unlike Array. If you add any incompatible element into an array, the compiler throws ArrayStoreException at run time. GenericĪn Array can’t be generic, it can hold a different type of element that checks on run time. It automatically grows or shrinks when we add or remove elements. When we create an ArrayList it has default size i.e. It will throw ArrayIndexOutOfBoundException.Īn ArrayList has the dynamic size, it’s a growable array. If we try to add more elements than the size of the Array. The most popular difference between ArrayList and Array is size.Īn Array has fixed length and we can’t change the length after the creation of Array object. Before moving further, you should read the Array in javaand ArrayList in java. In this post, we will discuss the difference between ArrayList and array. In java ArrayList and array, both are famous data structure.