How can you reverse an array without using the reverse()
method?
如何在不使用reverse()方法的情况下反转数组?
public class Main extends Sprite
{
private var _reversedList:Array = new Array();
public function Main()
{
var yourShoppingList:Array = ["Milk","Bread","Eggs","Cereal","Cheese","Ham"];
shoppingList(yourShoppingList);
trace("The original array was " + yourShoppingList + " and now it is reversed as " + _reversedList + ".");
}
private function shoppingList(items:Array):Array {
while(items.length){
var lastItem:String = items.pop();
_reversedList.unshift(lastItem);
}
return _reversedList;
}
}
This is what I have so far. I tried using _reversedList.unshift(items.pop());
but it was giving me an error, so I ended up creating a variable, and now it seems fine? But regardless, it's not reversing the Array, and I'm not sure why.
这就是我到目前为止所拥有的。我尝试使用_reversedList.unshift(items.pop());但它给了我一个错误,所以我最终创建了一个变量,现在看起来很好吗?但无论如何,它并没有扭转数组,我不知道为什么。
Thank you for your time and help. I really appreciate it.
感谢您的时间和帮助。对此,我真的非常感激。
*NOTE: For those of you who are Full Sail students, feel free to reference this, but don't copy / paste, as the teachers know about this post and you'll probably be penalized for cheating. Just a friendly warning.
*注意:对于那些全帆学生,请随意参考,但不要复制/粘贴,因为教师知道这篇文章,你可能会因作弊而受到惩罚。只是一个友好的警告。
1 个解决方案
#1
2
Changing
_reversedList.unshift(lastItem);
to
_reversedList.push(lastItem);
works for me. Taking the last item and pushing it in as the first item on the new array.
适合我。取最后一项并将其作为新阵列上的第一项推入。
#1
2
Changing
_reversedList.unshift(lastItem);
to
_reversedList.push(lastItem);
works for me. Taking the last item and pushing it in as the first item on the new array.
适合我。取最后一项并将其作为新阵列上的第一项推入。