Similar to the one below, another common recommendation would be which is better - ArrayList/Vector/Linked List. There are various factors to consider in saying which is better for example, if we consider retrieval of elements from the list - arraylist is faster than linkedlist but when you consider adding an element at a particular position say '0' then linked list is faster. Lets not go into details now. But overall, in a general scenario, its always recommended to use Arraylist in place of vector. But Will that really help??
Internally, both the ArrayList and Vector hold onto their contents using an Array. But when any new element is inserted into an ArrayList or a Vector, the object has to expand its internal array in case it is overbound. In that scenario, a Vector defaults by doubling the size of its array, while the ArrayList increases its array size by 50 percent. So, in case the arraylist or vector are not defined with initial capacity, the default size would be again 16characters. And then as new elements are being added, it keeps resizing itself apppropriately and you would end up taking a large performance hit.
So, It's always best to set the object's initial capacity to the largest capacity that your program will need. By carefully setting the capacity, you can avoid paying the penalty needed to resize the internal array later. If you don't know how much data you'll have, but you do know the rate at which it grows, Vector does possess a slight advantage since you can set the increment value.
Wednesday, October 15, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment