Lua uses one general-purpose structure—the table—to represent arrays, records, sets, linked lists, matrices, queues, and graphs. The best representation depends on access patterns rather than on a special declaration.
Arrays and records
1 | local colors = { "red", "green", "blue" } |
Dense arrays conventionally begin at 1. The length operator is reliable only when there are no unexpected holes; keep an explicit count for sparse collections.
Sets and multisets
1 | local set = { apple = true, orange = true } |
Remove a set member by assigning nil, not false, if iteration should omit it.
Matrices
A matrix can be a table of rows:
1 | local matrix = { |
For a large sparse matrix, store only populated coordinates, using nested tables or a serialized (row, column) key. A flat dense array may improve locality: index (row - 1) * columns + column.
Lists and queues
1 | local node = { value = "first", next = nil } |
Removing index 1 from an array repeatedly shifts every remaining element, so a first/last index queue is more efficient.
Graphs can map each node to a set or list of neighbors. Tables make all these models possible; document invariants such as indexing, ownership, ordering, and whether missing values differ from false.