Skip to main content
Lesson 20 - ArrayList
ZIPPDF (letter)
Lesson MenuPreviousNext
  
Array Implementation of a list page 3 of 9

  1. A data structure combines data organization with methods of accessing and manipulating the data. For example, an array becomes a data structure for storing a list of elements when we provide methods to find, insert, and remove an element. At a very abstract level, we can think of a general "list" object: a list contains a number of elements arranged in sequence; we can find a target value in a list, add elements to the list, and remove elements from the list.

  2. An abstract description of a data structure, with the emphasis on its properties, functionality, and use, rather than on a particular implementation, is referred to as an Abstract Data Type (ADT). An ADT defines methods for handling an abstract data organization without the details of implementation

  3. A "List" ADT, for example, may be described as follows:

    Data organization:
    - Contains a number of data elements arranged in a linear sequence

    Methods:
    - Create an empty List
    - Append an element to List
    - Remove the i-th element from List
    - Obtain the value of the i-th element
    - Traverse List (process or print out all elements in sequence, visiting each element once)

  4. A one-dimensional Java array already provides most of the functionality of a list. When we want to use an array as a list, we create an array that can hold a certain maximum number of elements; we then keep track of the actual number of values stored in the array. The array's length becomes its maximum capacity and the number of elements currently stored in the array is the size of the list.

  5. However, Java arrays are not resizable. If we want to be able to add elements to the list without worrying about exceeding its maximum capacity, we must use a class with an add method, which allocates a bigger array and copies the list values into the new array when the list runs out of space. That's what the ArrayList class does.

  6. The ArrayList class builds upon the capabilities of arrays. An ArrayList object contains an array of object references plus many methods for managing that array. The biggest convenience of an ArrayList is that you can keep adding elements to it no matter what size it was originally. The size of the ArrayList will automatically increase and no information will be lost.

  7. However, this convenience comes at a price:

    1. The elements of an ArrayList are object references, not primitive data like int or double.
    2. Using an ArrayList is slightly slower than using an array directly. This would be important for very large data processing projects.
    3. The elements of an ArrayList are references to Object. This means that often you will need to use type casting with the data from an ArrayList. "Type Casting" means to change the type of an object in order to conform to another use. See Part C, Object Casts, below.


Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.