Zac*_*Zac 1 actionscript-3 deque
我想要一种方法来存储最多三个字符串.当我得到一个新的,我想将它添加到列表的底部,并从列表的顶部删除一个(最旧的一个).
我知道这可以在带有双端队列的python中完成,但不知道如何在AS3中实现它或者它是否已经存在.谷歌搜索在googlecode上发现了一些代码,但它没有编译.
unshift() - 将一个或多个元素添加到数组的开头并返回数组的新长度.数组中的其他元素从其原始位置i移动到i + 1.
pop() - 从数组中删除最后一个元素并返回该元素的值.
var arr: Array = [ "three", "four", "five" ];
arr.unshift( "two" );
trace( arr ); // "two", "three", "four", "five"
arr.unshift( "one" );
trace( arr ); // "one , ""two", "three", "four", "five"
arr.pop(); //Removes the last element
trace( arr ); // "one , ""two", "three", "four"
Run Code Online (Sandbox Code Playgroud)
所以在你的情况下:
"我想将它添加到列表的底部,并从列表顶部删除一个(最旧的一个)."
var arr: Array = [ "my", "three", "strings" ];
arr.unshift( "newString" ); //add it to the bottom of the list
arr.pop(); // remove the one from the top of the list (the oldest one)
Run Code Online (Sandbox Code Playgroud)
您将在数组中有3个字符串,您可以像这样访问它们:
trace( arr[0] ); //first element
trace( arr[1] ); //second element
trace( arr[2] ); //third element
Run Code Online (Sandbox Code Playgroud)
因为您只想存储字符串,所以可以使用Vector来获得更好的性能.
简而言之,Vector类是一个'类型化数组',并且具有与Array类似的方法.
您案件的唯一区别在于声明:
var vect: Vector.<String> = new <String>[ "my", "three", "strings" ];
vect.unshift( "newString" ); //add it to the bottom of the list
vect.pop(); // remove the one from the top of the list
Run Code Online (Sandbox Code Playgroud)