Solidity Array, Structures, Mapping, Enumeration

array

Arrays can be declared with a specified length or dynamically resized. Element types can be arbitrary for arrays that store storage s (that is, elements can also be array types, mapping types, or structs). Element types cannot be mapping types for arrays of memory memory memory, and can only be ABI types if they are parameters to the public function.
An element of type T with a fixed length of K can be declared as T[k], while a dynamic array can be declared as T[]. For example, an array of dynamic arrays with a length of 5 and an element type of uint, To access the second element of the third dynamic array, you should use x[2][1] (array subscripts start at 0 and are accessed in the opposite order as they are declared, that is, x[2] is reduced by one level from the right).

contract array {
    uint256[] public arr; // Indefinite long array
    uint256[10] public fixedArr; // Fixed-length array
    uint256[] public arr2 = [1,32,3,221,12,12,12123123]; 

    // add value
    function pushArr(uint256 _a) public {
        arr.push(_a);
    }
    
    // Delete Value
    function popArr() public {
        arr.pop();
    }

    // Delete the specified location value
    function del(uint256 _index) public {
        delete arr[_index];
    }
}

Mapping

Mapping types are declared as mapping (_KeyType => _ValueType). Where _ KeyType can be almost any type except mapping, variable-length arrays, contracts, enumerations, and structs. ValueType can be any type including mapping type.
Mapping can be thought of as a hash table https://en.wikipedia.org/wiki/Hash_table , they create each possible key during the actual initialization process and map it to a byte-style, all-zero value: the default value for a type. However, here's what makes a map different from a hash table: instead of actually storing a key in a map, it stores its keccak256 hash value, which makes it easier to query the actual value.
Because of this, there is no length for a map, and there is no concept of a set of key s or a set of value s.
Only state variables (or references to stored variables in the internal function) can use mapping types.
You can declare the mapping public and let Solidity create a getter. _ KeyType becomes a required parameter of the getter, and the getter returns _ ValueType.
_ ValueType can also be a map. When using getter s, you will need to pass in each _recursively KeyType parameter.

contract Test {

    mapping (address => uint256) public balanceOf;

    function getbalance(address _address) public view returns (uint256) {
        return balanceOf[_address];
    }

    function setBalance(address _address, uint256 _amount) public {
        require(_amount>0,'amount miss > 0');
        balanceOf[_address] += _amount; 
    }

    function remove(address addr) public {
        delete balanceOf[addr];
    }
}

Enum

contract test {
    enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
    ActionChoices choice;
    ActionChoices constant defaultChoice = ActionChoices.GoStraight;

    function setGoStraight() public {
        choice = ActionChoices.GoStraight;
    }

    // Since the enumeration type is not part of |ABI|, for all calls from outside Solidity,
    // The signature of "getChoice" is automatically changed to "getChoice() returns (uint8)".
    // Integer types are large enough to store values of all enumeration types, and as the number of values increases,
    // Increasingly, `uint16'or larger integer types can be used.
    function getChoice() public view returns (ActionChoices) {
        return choice;
    }

    function getDefaultChoice() public pure returns (uint) {
        return uint(defaultChoice);
    }
}

structural morphology

pragma solidity ^0.8.0;

contract CrowdFunding {
    
    struct Funder {
        address addr;
        uint256 amount;
    }
    uint num;
    mapping (uint256 => Funder) public funder;

    function set(address addr, uint256 amount) public {
        funder[num] = Funder(addr,amount);
        num++;
    }
}

Keywords: Blockchain

Added by ajfton on Sun, 02 Jan 2022 04:31:21 +0200