当前位置:网站首页>Lua removing elements from a loop in a list
Lua removing elements from a loop in a list
2022-06-11 03:38:00 【sayWhat_ sayHello】
Preface
stay code I thought of this case :
local t = {
1, 2, 3, 4, 5}
for i = 1, #t do
if i == 3 then
table.remove(t, i)
end
end
for i = 1, # t do
print(t[i])
end
I don't know LUA What is the effect of the program . Because it is not usually supported to delete elements in a loop .
This operation may throw exceptions in other languages . For example, in JAVA This operation is not supported in , Only in the iterator can we correctly delete .
Conclusion
Run the , Found it can run normally :
1
2
4
5
Change the code above :
local t = {
1, 2, 3, 4, 5}
for i = 1, #t do
if i == 3 then
table.remove(t, i)
end
print(t[i])
end
The result is as follows :
1
2
4
5
nil
It can be found that when the coordinates are 3 The element of has been removed , Subsequent elements move forward .
A clever way
As mentioned above, such deletion will cause the following elements to move forward , But this traversal is followed by nil, Does not conform to the logic of the program .
A trick is to turn it upside down :
local t = {
1, 2, 3, 4, 5}
for i = #t, 1, -1 do
if i == 3 then
table.remove(t, i)
end
print(t[i])
end
Output :
5
4
4
2
1
You can press here to output two 4, Because 3 Deleted , Put it in 3 The position is now 4.
In business logic This situation is equivalent to determining whether to delete more than once . Correct scheme .
If you have done other operations besides judging whether to delete, you should be careful with this method .
边栏推荐
- Canvas drawing -- how to place the drawing in the center of the canvas
- ARM开发板方案与厂商分析
- 多线程四部曲之NSThread
- C. Jump and Treasure(dp + 单调队列优化)
- 潮玩力真火力!年轻人第一台巨幕影院?酷开电视Max 86“庞然来袭
- OpenGL第七章 基础光照
- Image scaling with aspect ratio preserving by opencv
- Implementation of publish and subscribe mode ----- hand tearing JS series
- /10个值得推荐的学习编程的网站 世界已经进入了互联网的时代。据最近发布的一篇《2016年互联网趋势》报告显示,中国已成为互联网市场的领导者,中国互联网用户的数量达到了6.68亿。可以预见,有
- canvas旋转绘图h5动画js特效
猜你喜欢

J. Balanced Tree

Pthread in the multithreaded Trilogy

JS top icon menu click to switch background color JS special effect

Canvas+svg line particle animation web page background

Lvgl Chinese font production
![[cloud native] what is micro service? How to build it? Teach you how to build the first micro service (framework)](/img/2c/50c692e090d64ab67f7501beb1d989.png)
[cloud native] what is micro service? How to build it? Teach you how to build the first micro service (framework)

OpenGL error Guide

多线程四部曲之pthread

Canvas interactive star animation background JS special effect

正则表达式
随机推荐
OpenGL error Guide
被“内卷”酸翻的OPPO Reno6
蓄力618 ,苏宁如何打下这场硬仗?
has been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘ header is present on the requested
Mavros controls UAV to conduct binocular slam in gazebo environment
Lecturer paging query_ Instructor condition query with page
The tide play power is really firepower! The first big screen cinema for young people? Cool open TV Max 86 "sudden attack
摘桃子(双指针)
基于SSM框架的连锁超市购物零售后台管理系统
JS click the sun and moon to switch between day and night JS special effect
OpenGL Chapter 8 material
/The world of 10 recommended websites for learning programming has entered the era of the Internet. According to a recently released Internet trends 2016 report, China has become a leader in the Inter
SQL | some indicators of the game industry
OPENSSL ASN. 1, DER, PEM, X509
PostgreSQL source code learning (21) -- fault recovery ② - transaction log initialization
Logical deletion_ Swagger2 framework integration
JS the most commonly used sorting - hand tearing JS series
右键 powershell here 功能添加
OpenGl第十章 投光物
Free flying animation of paper plane based on SVG