Lecture

Storing Data Sequentially with Array

An array is a data structure that stores multiple data elements of the same type in order, similar to a list in Python like [1, 2, 3].

Each element can be accessed using an index, which typically starts from 0.

String Array Example
my_array = ["apple", "banana", "cherry"]

Characteristics of an Array

An array data structure has the following characteristics.


Fixed Size

An array generally has a fixed size.

You must specify the size when creating an array, as it is allocated contiguously in memory.

Python's list overcomes this limitation by internally re-allocating and copying to dynamically adjust its size.


Elements of the Same Type

Generally, an array is composed of elements of the same data type.

For example, an integer array only stores integers, and a string array only stores strings.

This structure allows for efficient memory usage and simplifies the calculation of each element's memory address.

In contrast, Python's list is similar to an array but is not size-constrained, allows dynamic changes, and can contain elements of different data types.

Such a structure is referred to as a Dynamic Array.


Fast Access via Index

Each element in the array has a unique index (usually starting at 0), allowing access to any element with a constant time complexity, i.e., O(1).

Arrays allow for very fast access to data if the position of the element is known.


Example Usage of Arrays

The following code demonstrates using Python's list data structure to create an integer array of size 5, adding elements, and modifying elements.

Python Array Example
my_array = [10, 20, 30, 40, 50] # Access specific element of the array print(my_array[2]) # Output: 30 # Get the length of the array print(len(my_array)) # Output: 5 # Add an element to the array my_array.append(60) # [10, 20, 30, 40, 50, 60] # Modify a specific element of the array my_array[1] = 25 # [10, 25, 30, 40, 50, 60] # Iterate over the elements of the array for element in my_array: print(element)
Mission
0 / 1

What is the most appropriate word to fill in the blank?

In general, the of an array is fixed, so its size must be specified when the array is created.
index
type
size
address

Lecture

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result