Arrays, lists and sequences
Ordered collections indexed by position: arrays, dynamic arrays, linked lists, slices and ranges.
Concepts
- Array indexing
Array indexing selects an item of an array by its position. Most languages count from 0, some count from 1, and many check that the index is inside the array.
intro - Range
A range is a sequence of numbers or positions given by a start, an end and sometimes a step. Most languages include the start and exclude the end.
intro - Array
An array is a collection of items of one type that sit side by side in memory, so the program finds item i by a computation on i. A plain array has a fixed length.
core - Dynamic array
A dynamic array keeps its items in an ordinary array with spare slots at the end. When the slots are full, it makes a bigger array and copies the items, so an append takes constant time on average.
core - Linked list
A linked list keeps each item in its own node with a link to the next node. It inserts and removes at a known node in constant time, but it reaches item i only by following i links.
core - List
A list is a finite collection of items in order, where one value can appear more than once. It is a dynamic array in some languages and a linked list in others.
core - Multidimensional array
A multidimensional array selects an item by two or more indices, such as a row and a column. It is either one block in row-major or column-major order, or an array of separate rows.
core - Sequence
A sequence is a set of items in a fixed order, where one value can appear more than once. In code the word names indexed types, ordered collections and lazy streams of items.
core - Slice
A slice is a run of consecutive items of an array or a list, chosen by a start and an end index. Some languages copy the items, and others give a view that shares the memory.
core
Tasks
- Append to a list
Add one item to a list, at the end with append, push or Add, or at a given position with insert, and know what each call returns and when it fails.
10 languagesintro - Check whether a list contains a value
Tell whether a value is one of the items of a list, and know which equality each language uses, how NaN and object items behave, and when a set is the better structure.
10 languagesintro - Concatenate two lists
Make a new list with the items of one list followed by the items of another, or add the second list to the end of the first in place.
10 languagesintro - Create a two-dimensional array
Make a grid of rows and columns filled with one value, with a separate list for each row, and avoid the rows that share one list.
10 languagesintro - Create an array
Make an array of a given length, with listed values or with one value in each slot, as a fixed array or as a growable one.
10 languagesintro - Find the index of a value
Find the first position of a value in a list, or a not-found result, and know how each language reports a value that is not there, -1, an error, an end iterator or an empty option.
10 languagesintro - Get the length of an array
Get the number of items in an array or list with len, length, size or Count, and know why sizeof and a pointer give a wrong count in C.
10 languagesintro - Reverse a list
Make a new list with the items of a list in the opposite order, and know which reverse functions change the list in place, which return a view, and which return a lazy iterator.
10 languagesintro - Slice a list
Copy the items from a start position up to, but not including, a stop position into a new list, and know which languages copy, which share storage, and which clamp a bad range.
10 languagesintro - Use a linked list
Push and pop at both ends of a linked list and remove a node in the middle, with the standard list type or with nodes that you build, and see why a Rust list is hard to write.
10 languagescore