Array: Difference between revisions
More actions
new |
(No difference)
|
Latest revision as of 13:26, 9 August 2025
An array is simply a collection of values. They are stored next to each other in memory so can be accessed directly using their memory address (plus an offset to get the value at a specific index).
Array data types
edit edit sourceArrays must contain values of only one data type.
For example, this array would be fine as it only contains integers:
[4, 9, 20, 50, 299]
However this array would not be, as it contains both strings and booleans:
["hello", "world", true, "Tokyo", false]
Some dynamically-typed programming languages, such as Python, allow you to have values of multiple data types in a single list. No statically typed programming language offers this.
Common array + collection methods
edit edit sourceThere are some common methods or fields you can access on arrays, lists and other collections in most programming languages. They may have different names to those listed below, but should be present.
- get(index) - to get the value at a specific index
- set(index, value) - to set the value at the specified index
- append(value) - to add a value to the end of a collection (usually only works with dynamically-typed lists as with static typing, array lengths are fixed)
- length - to get the number of elements in the collection