从数组中弹出第一个元素 [英] Popping the first element off an array

查看:226
本文介绍了从数组中弹出第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Lua中有一个数组x.我想设置数组其余部分的head = x[1]rest =,以便rest[1] = x[2]rest[2] = x[3]等.

I have an array x in Lua. I would like to set head = x[1] and rest = the rest of the array, so that rest[1] = x[2], rest[2] = x[3], etc.

我该怎么做?

(注意:我不在乎原始数组是否发生突变.在Javascript中,我会做head = x.shift(),而x会包含其余元素.)

(note: I don't care if the original array gets mutated. In Javascript I would do head = x.shift() and x would contain the remaining elements.)

推荐答案

head = table.remove(x, 1)

"Pop"有点用词不当,因为它暗示着便宜的操作,并且删除表的第一个元素需要重新定位其余的内容-因此,JavaScript和某些其他语言中的名称为"shift".

"Pop" is a bit of a misnomer, as it implies a cheap operation, and removing the first element of an table requires relocating the rest of the contents--hence the name "shift" in JavaScript and some other languages.

这篇关于从数组中弹出第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆