Data Structures and Algorithms using Python
Copyright © 2023 by Rance Necaise
Table Of Contents
Data Structures and Algorithms using Python
Copyright © 2023
by Rance Necaise

3.1.1 The Array ADT

The 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.

ADT Definition

The Array ADT

A one-dimensional array is a collection of contiguous elements in which individual elements are identified by a unique integer subscript starting with zero. Once an array is created, its size cannot be changed.

  • Array(size)

    Creates a one-dimensional array consisting of size elements with each element initially set to None. size must be greater than zero.

  • length()

    Returns the length or number of elements in the array.

  • getitem(index)

    Returns the value stored in the array at element position index. The index argument must be within the valid range. Accessed using the subscript operator.

  • setitem(index,

    Modifies the contents of the array element at position index to contain value. The index must be within the valid range. Accessed using the subscript operator.

  • clear(value)

    Clears the array by setting every element to value.

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.

Page last modified on July 25, 2023, at 01:48 PM