3.1 The Array StructureAt the hardware level, most computer architectures provide a mechanism for creating and using one-dimensional arrays. A one-dimensional array, as illustrated in Figure 3.1.1, is composed of multiple sequential elements stored in contiguous bytes of memory and allows for random access to the individual elements. The array structure is another example of a mutable indexed ordered sequence in which the elements are arranged in linear order from front to back and the elements of the sequence are accessible by position. As you may remember, Python provides several indexed sequences, two immutable indexed sequences, strings and tuples, and one mutable indexed sequence, the list. The entire contents of an array are identified by a single name. Individual elements within the array can be accessed directly by specifying an integer subscript or index value, which indicates an offset from the start of the array. This is similar to the mathematics notation , which allows for multiple variables of the same name. The difference is that programming languages typically use square brackets following the array name to specify the subscript, ![]() Figure 3.1.1: A sample 1-D array consisting of 11 elements.
The Array ADTThe array structure is commonly found in most programming languages as a primitive type, but Python only provides the list structure for creating mutable sequences. We can define the Array ADT to represent a one-dimensional array for use in Python that works similarly to arrays found in other languages. It will be used throughout the text when an array structure is required. The Array ADT
Some computer scientists consider the array a physical structure and not an abstraction since arrays are implemented at the hardware level. But remember, there are only three basic operations available with the hardware-implemented array. As part of our Array ADT, we have provided for these operations but have also included an iterator and operations for obtaining the size of the array and for setting every element to a given value. In this case, we have provided a higher level of abstraction than that provided by the underlying hardware-implemented array.
|