Solidity-Array数组的长度维护,如何彻底删除元素.

​问题描述

在链上开发中,经常会有一些业务,例如需要存储多个地址,并且我可以对这多个地址进行查看length,还有指定下标获取某个值,又或者,我需要删除指定的下标,而且length要更新。现在我问题来了,Array删除指定下标的值之后,此下标的值会变成默认值,并没有实现真正我们想要的业务删除,那么如何处理呢?

解决方案:

解决上面的问题一般有两种实现方式:

  1.  删除,然后把删除的元素for循环到最后一个位置,弹出pop。
  2.  替换,将要删除的元素和最后一个元素替换,然后弹出pop。

两种适用场景:

  1. 第一种 考虑数组顺序,这种做法不打乱顺序,但是消耗gas
  2. 第二种 不考虑顺序,只考虑长度,而且节省gas
代码:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.6;
contract ArrayRemoveByShifting {
uint[] public arr;

    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];
        }
        arr.pop();
    }

    function test() external {
        arr = [1, 2, 3, 4, 5];
        remove(2);
        // [1, 2, 4, 5]
    }
/**-----------------------以上是第一种方式---------------------------------**/

    function remove2(uint index) public {
        // Move the last element into the place to delete
        arr[index] = arr[arr.length - 1];
        // Remove the last element
        arr.pop();
    }

    function test() public {
        arr = [1, 2, 3, 4];
        remove2(1);
        // [1, 4, 3]
    }
/**-----------------------以上是第二种方式---------------------------------**/

}

以上的解决方案可以根据自己的业务自行选择一个最优的。

猜你喜欢

转载自blog.csdn.net/qq_33842966/article/details/125185064