Deciding between arrays and mappings
Arrays and mappings in solidity are the most commonly used data types to store complex data. There are trade-offs in using one over the other, and each has its own advantages. In this recipe, you will learn about common use cases for arrays
and mappings
.
How to do it...
Arrays
and mappings
are used for a series of objects, all of which are the same size and type. Arrays store data sequentially with an incrementing index, whereas mappings store data as key-value pairs (which can also be seen as hash tables).
Arrays
Follow these steps to create arrays in solidity and to perform read/write operations:
- Arrays can be created with a fixed or dynamic length. Arrays can also hold user-defined types:
uint[10] numbers; struct Person { uint id; string name; } Person[] candidates;
- The creation of multidimensional arrays is supported by solidity:
// array of 3 dynamic arrays of bool bool[][3] table;
Note that the index value is reversed when compared with other languages...