Introduction
What are 1 dimensional arrays and what are they used for?
1 dimensional arrays are a form of compound data type that are used to store a list of items in order. Sometimes the ordering of items may not be important (such as a shopping list) but sometimes it can be crucial (Names of runners collected as the finish a race).
They are highly useful if you need to:
- Search through multiple items to see if a particular example exists/fits certain criteria.
- Perform the same action for each item in the list
- Need to find out the relative position of various items in the list (for instance find out the difference between the finishing positions or runners in a race)
Arrays vs Lists
Arrays vs lists in Python
- Python natively uses lists instead of arrays (you can use Numpy arrays if you need to)
- Python lists are very simple to use and highly flexible
- They can contain heterogeneous data(lots of different types of data)
- They make less efficient use of memory than arrays
- Certain maths/data science uses require the use of arrays instead of lists, but this is rare in general programming)
Array Terminology
Traversing / Iterating over arrays
The most common action to take on an array is to go through each item in an array and performing an action on the item (for instance printing the item out, or checking if it matches certain criteria). This is known is array traversal or Iterating over/through an array.
Static vs Dynamic arrays
Some language implement static arrays, where the size or shape of the array cannot be changed once it has been declared. If you want to change an existing array you have to make a copy of it.
Other language implement dynamic arrays, where you can change the size of the array after it has been declared.
Index / Indices
This can apply to the index of a particular item in an array (e.g. The index of the element “dog” is 4) or to can be used generally to describe the manner in which array items are counted (e.g. Python lists index from 0).
Upper and Lower Bounds
The lower bound is the index of left most item in a array, the upper bound is the index of the right most item in an array. A more detailed explanation can be found on the Upper/Lower Bounds/Slicing page.
Slicing
Extracting a portion of an array using start and end indexing to determine the bounds of the extraction. In Python this works in a similar way to string slicing. More information can be found on the Upper/Lower Bounds/Slicing page.
Extent
The number of elements in a particular dimension. In single dimensional array it is the number of items in the array.
Append / Insert / Push
These are terms for the various ways in which you can add to an array or list. These are implemented different depending on the language that you are programming language.
Pseudocode
CIE Array Pseudocode
Python
Python List Common Features
Create lists
Appending to lists
Removing items from lists
Combining lists
Deleting items
Finding items in lists
Traversing lists