a guest Nov 20th, 2018 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- The fundamental differences between Array and ArrayList are the same as those between Array and Vector. Both ArrayList and Vector are generic wrapper classes containing a private array instance, whose methods to add or remove elements of the private array will increase or decrease respectively the size of the array (and therefore the memory it requires). This is done by checking when adding or removing items whether the array is of the appropriate size given the number of elements it contains, create a new array of an appropriate size, copying each element to the new array, and updating its private array property to reference the new array. They also allow the user to preemptively grow or shrink the array as a performance optimization if the user can anticipate how many elements will be added, or if there are no more elements to add.
- The primary practical difference between Vectors and ArrayLists is in their use in multithreaded programs. Vectors are “synchronized”, meaning that their mutating methods are all implemented with the “synchronized” keyword, and so are guaranteed to work in multithreaded programs without errors. The synchronized keyword does cause a performance penalty, so if the built-in synchronized methods are unnecessary for the program being written, the program may run slower than with an ArrayList used in place of a Vector. A program using ArrayList may still make use of the synchronized keyword.
RAW Paste Data