- var my_array:Array=['1','2','3','4','5'] //le tableau que l'on veut faire tourner
- // SHIFT ARRAY TO RIGHT (dernier élément devient le premier)
- my_array.unshift(my_array.pop());
- trace(my_array.join('-'));
- // SHIFT ARRAY TO LEFT (premier élément devient le dernier)
- my_array.push(my_array.shift());
- trace(my_array.join('-'));
-
- et pour ceux qui aiment les classes (comme moi)
-
- class arrayRotation {
- // Private constructor
- private function arrayRotation() {}
- // Static function Rotate
- // theArray : l'Array qu'on souhaite manipuler
- // si theArray n'est pas défini, une nouvelle Array vide est renvoyée
- // _direction : chaine de caractère indiquant la direction ('left' ou 'right')
- // si _direction non défini ou ni 'left' ni 'right' l'Array d'origine est renvoyée
- static function Rotate(theArray:Array,_direction:String):Array
- {
- if (theArray==undefined) return new Array();
- _direction=_direction.toUpperCase();
- // Rotation of array
- switch(_direction)
- {
- case 'RIGHT':
- // SHIFT ARRAY TO RIGHT
- theArray.unshift(theArray.pop());
- break;
- case 'LEFT':
- // SHIFT ARRAY TO LEFT
- theArray.push(theArray.shift());
- break;
- }
- return theArray
- }
- }
var my_array:Array=['1','2','3','4','5'] //le tableau que l'on veut faire tourner
// SHIFT ARRAY TO RIGHT (dernier élément devient le premier)
my_array.unshift(my_array.pop());
trace(my_array.join('-'));
// SHIFT ARRAY TO LEFT (premier élément devient le dernier)
my_array.push(my_array.shift());
trace(my_array.join('-'));
et pour ceux qui aiment les classes (comme moi)
class arrayRotation {
// Private constructor
private function arrayRotation() {}
// Static function Rotate
// theArray : l'Array qu'on souhaite manipuler
// si theArray n'est pas défini, une nouvelle Array vide est renvoyée
// _direction : chaine de caractère indiquant la direction ('left' ou 'right')
// si _direction non défini ou ni 'left' ni 'right' l'Array d'origine est renvoyée
static function Rotate(theArray:Array,_direction:String):Array
{
if (theArray==undefined) return new Array();
_direction=_direction.toUpperCase();
// Rotation of array
switch(_direction)
{
case 'RIGHT':
// SHIFT ARRAY TO RIGHT
theArray.unshift(theArray.pop());
break;
case 'LEFT':
// SHIFT ARRAY TO LEFT
theArray.push(theArray.shift());
break;
}
return theArray
}
}