Explore BrainMass

Explore BrainMass

    ArrayList

    The arraylist, as it is called in Java, goes by many other names too: dynamic array, growable/resizable array, mutable array, list (Python), vector (C++, Ada), ordered collection (Smalltalk) and in some cases (PHP, Javascript, etc.) it simply shares the name 'array'. What  set the arraylist apart from the static arrays of C, Java, old PASCAL and many other languages, is its ability to grow and shrink as needed at runtime. This means one can add and remove elements freely over the course of the program without fear of segmentation faults or otherwise overrunning the array's alloted space in memory. Most languages also support multidimensional arraylists.

    Most modern mainstream programming languages include some way to implement an arraylist-type structure

    Like any array, arraylists are bolstered up in the ranks of data structures by their random access property (sometimes also called direct access) which allows elements to be accessed in O(1) (constant) time. This gives them the edge over linked lists, etc. for getting and overwriting elements, but adding/removing elements from anywhere but the end of the array requires shuffling the remaining elements to compensate in O(n) (linear) time (whereas a linked list will do it in constant time) so the choice must depend on the intended application.

    Common operations on arraylists include:

    • searching
    • sorting (they are compatible with quicksort, mergesort, bubblesort and many others)
    • appending elements
    • 'popping' elements off the end
    • adding/removing elements from somewhere other than the end
    • iterating through elements to change/check a property
    • replacing data at a particular index
    • clearing the arraylist
    • copying the arraylist
    • finding the size of the arraylist
    • checking if the arraylist is empty

    There are times when a programmer can be sure they will never need more than a certain number of elements in their array, but for all the other times, arraylists are highly useful structures. 

    © BrainMass Inc. brainmass.com April 25, 2024, 3:22 pm ad1c9bdddf