当前位置:网站首页>How to maintain the length of a solid array and how to delete elements completely
How to maintain the length of a solid array and how to delete elements completely
2022-06-10 07:21:00 【Zeke Luo】
Problem description
Developing on the chain , There are often some businesses , For example, you need to store multiple addresses , And I can view these multiple addresses length, You can also specify a subscript to get a value , Or, , I need to delete the specified subscript , and length To update . Now my question is ,Array After deleting the value of the specified subscript , The value of this subscript becomes the default value , The business deletion we really want is not realized , So how to deal with it ?
Solution :
There are generally two ways to solve the above problems :
- Delete , Then delete the element for Cycle to the last position , eject pop.
- Replace , Replace the element to be deleted with the last element , Then the pop-up pop.
Two applicable scenarios :
- The first one is Consider array order , This does not disturb the order , But consumption gas
- The second kind Regardless of order , Length only , And save gas
Code :
// 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]
}
/**----------------------- The above is the first way ---------------------------------**/
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]
}
/**----------------------- The above is the second way ---------------------------------**/
} The above solutions can choose the best one according to their own business .
边栏推荐
- 3 zk的选举机制
- How to install and use NPM
- P1073 [noip2009 improvement group] optimal trade problem solving hierarchical graph shortest path
- OpenCV学习(二)---树莓派上安装opencv
- You can have zongzi if you want, but you have to go through my authentication and authorization
- Around ifelse and business logic
- autojs与冰狐智能辅助的优缺点
- 电容隔离原理
- 【宽度优先搜索】LeetCode1091二进制矩阵中的最短路径
- Create RT thread software simulation project and write RT thread kernel
猜你喜欢
![[C language] C language programming: dynamic address book](/img/bb/b44066e91219a57b653e330198e4a0.png)
[C language] C language programming: dynamic address book

Chenxi bookkeeping book for bookkeeping, and use items to view accounts

2. ZK's working mechanism

Refresh 54 Chinese NLP task benchmarks at one stroke. Easydl under ernie3.0 may be the best NLP development platform on the market

29. solution for 300ms delay time of click event at mobile terminal

Nationwide provincial and municipal linkage JSON data

The title of my composition is - "my district head father"

How to modify photos of downloaded word resume templates

Instagram CEO: "Apple iPad is not very popular, and it is not worth developing an exclusive version of the app"

Solution: the go language item in vscode cannot be automatically imported
随机推荐
All in one 1258 Dynamic programming of digital pyramid problem solving
Go+vue+pgsql- family management system project conclusion
Local storage of JS data interaction
Arena, a new proposal of the go language community, can optimize memory allocation
Summary of technical scheme for automatic wool picking
R 18 cutting replacement exercise
Intelligently merge videos, randomly merge video covers, and preset new titles
Create RT thread software simulation project and write RT thread kernel
Markdown MD file editor test instructions
【C语言】C语言程序设计:动态通讯录
Rk3399 default browser and Chinese language
bson,json
Keyboard events and form events
Custom annotations support spiel expressions (dynamic parameters)
正则表达式 常用的正则规则汇总
Solution: the go language item in vscode cannot be automatically imported
8-1不安全的文件下载原理和案例演示
【计量经济学】工具变量估计与两阶段最小二乘法
创建RT-thread软件仿真工程 写RT-thread内核
Using the traversal sequence of the first and middle order of the binary tree to create the binary tree again