Tuesday, January 6, 2015
Arrays vs. Arraylists
While similar in name and basic functionality, arrays and arraylists have several striking differences that must be taken into account before being used by a programmer. While arrays can contain any object or datatype, such as characters and integers, arraylists can only contain objects. In addition, arraylists aredynamic in size, meaning that they can expand or shrink depending on the amount of entries within them. This is unlike arrays, which can be set to a certain size but not easily changed after that point without creating a new array to overwrite the original. Due to both of these differences, the method of creating each is slightly different, as the arraylist does not require brackets and will not accept a length to define it. In fact, arraylists use the size() method to calculate size rather than the length() method used by datatypes such as strings or arrays. To work with the dynamic size of arraylists, there is an additional add() method that allows for a new entry in the arraylist. The lack of finite size allows for arraylists to function similarly to their counterparts, but also adapt and remain usable even when the amount of data to be stored is uncertain when first creating it. The inability to utilize primitives is a drawback, but not much of one, as these primitives could easily be stored within the objects that are placed in the arraylist. This makes me assume that arraylists were added to increase organization while also allowing for flexibility and the same functionality of arrays.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment