I’m glad that explanation helped. You may have noticed that I removed my original reply. That’s because I needed to correct it for one inaccuracy that I realised afterwards. So I’m reposting a new improved version, here, now (see below). Basically, I said mappings don’t store the actual values, but actually they do — they just store them in a different way to arrays. Everything else about the explanation and analogy is broadly correct, at least to get a basic understanding of the differences between mappings and arrays. Please, be aware that this explanaation is oversimplified, but that’s often what we need when we first try to understand these concepts. Then it becomes a journey of discovery, where you will gradually realise the finer details for yourself, over time. At least this is a start…
The other thing I’m updating the explanation for, is the fact that arrays store their values based on a sequence of indices. So the key/value pairs are effectively index/value pairs. With mappings, there is no order to the values, just key/value pairs (e.g. address/value), so you need to provide the specific key that is mapped to the value, in order to access it.
Anyway, here’s the updated explanation:
Think of an array as being like a filing system/cabinet. You can search around in it, retrieve data from specific places in it, and you can iterate over it. All of the values stored in an array are like the contents/data in each of the files in a filing cabinet. These values (file contents) are ordered according to a sequence of indices (0, 1, 2, 3 etc.), a bit like filing reference numbers. Each index (file reference number) and it’s associated value (file contents/data) is a key/value pair (the key being the index).
A mapping is more like a telephone operator (bear with me…). If you give it a key (whatever the key has been defined as — let’s say a telephone number for this analogy, but in Solidity it’s more likely to be an address)… with that key (telephone number) the mapping will supply you with the value that the key is mapped to (the telephone operator will connect you to the person who is “mapped” to the telephone number you gave them).
The telephone operator (mapping) can only connect you to the other person (return the value) if you already have their telephone number (key/address). With a filing cabinet (array) you can hunt around (manipulate it / iterate over it etc.) until you find what you’re looking for, because of its ordered sequence of reference numbers (indices). Generally speaking, you can’t iterate over a mapping due to it’s non-sequential nature. However, if you have the key to a value (the telephone number of the person you want to speak to and interact with) the mapping (telephone operator) can “put you through” straight away. If you have very large mappings this will save you a noticeable amount of gas (as opposed to using an array). The advantage of an array is its flexibility, whereas a mapping will give you more privacy (you don’t have to speak to a load of other people until you find the one you actually want to talk to).
Wow! That’s a lot longer than the first draft! But it’s more accurate and will hopefully be of help to other Solidity beginners too. I hope, as well, that’s it’s enhanced your understanding, and not caused a slip back into confusion.
Let me know what you think.
I’m going to look at, and answer, your other question now. Thanks for your patience and for bearing with me.
Not at all! That’s what we’re here for