Solidity0.8.0-数组删除元素通过移动位置

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.7;

contract ArrayShift {
    uint[] public arr;

    function example() public {
        arr = [1,2,3];
        delete arr[1]; //[1,0,3]
    }

    // [1,2,3] -- remove(1) -->[1,3,3] -->[1,3]
    // [1,2,3,4,5,6] -- remove(2) --> [1,2,4,5,6,6] -->[1,2,4,5,6]
    // [1] -- remove(0) --> [1] -->[]
    function remove(uint _index) public {
        require(_index < arr.length, "index out of bound"); //索引值不能比数组长度大

        for (uint i = _index; i < arr.length - 1; i++){
            arr[i] = arr[i + 1];//数组元素向左移1
        }
        arr.pop();//让最后一个值弹出
    }

    function test() external {
        arr = [1,2,3,4,5];
        remove(2);
        //[1,2,4,5]
        assert(arr[0] == 1);
        assert(arr[1] == 2);
        assert(arr[2] == 4);
        assert(arr[3] == 5);
        assert(arr.length == 4);

        arr = [1];
        remove(0);
        // []
        assert(arr.length == 0);
    }
}

 

posted @ 2022-07-27 12:38  ZaleSwfit  阅读(146)  评论(0编辑  收藏  举报