在for循环中,使用pairs()和ipairs()循环有什么区别?此页面同时使用:Lua文档
使用ipairs():
a = {"one", "two", "three"}
for i, v in ipairs(a) do
print(i, v)
end
Run Code Online (Sandbox Code Playgroud)
结果:
1 one
2 two
3 three
Run Code Online (Sandbox Code Playgroud)
使用pairs():
a = {"one", "two", "three"}
for i, v in pairs(a) do
print(i, v)
end
Run Code Online (Sandbox Code Playgroud)
结果:
1 one
2 two
3 three
Run Code Online (Sandbox Code Playgroud)
您可以在这里进行测试:Lua演示