DEV Community

Anonloopdev.eth
Anonloopdev.eth

Posted on

Arrays in solidity for complete beginners

When you want a collection of something,you can use an array. There are two types of arrays in solidity.

  1. Fixed arrays.
  2. Dynamic arrays.

// Array with a fixed length of 2 elements.
Uint[2] fixed array;
//Another fixed array, can contain 5 strings:
String [5] string array;
// A dynamic array- has no fixed size , can keep growing and increasing.
Uint[ ] dynamic array;
Person [ ] people; // dynamic array,we keep adding.
Note:

  • Remember that state variables are stored permanently in the Blockchain?
  • So creating a dynamic array of structs like this can be useful for storing structured data in your contract, kind of a database.

PUBLIC ARRAYS
You can declare an array as public, and solidity will automatically create a getter method for it. The syntax looks like:
Person [ ] public people.
So other contracts could be able to read from, but not write to, this array. So this is a useful pattern for storing public data in your contract.

Top comments (0)