View Single Post
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 09-14-2016 , 01:40   Re: Rotating an Array?
Reply With Quote #9

untested ArrayList rotating function

PHP Code:
//assuming that we already have a populated ArrayList

//passing a positive value to the second parameter causes items at the END of the array to be moved to the front of the array
//passing a negative value causes items at the beginning to be placed at the end
stock void ArrayList_Rotate(ArrayList list, int amount) {
    if (list == 
null || amount == 0) return;
    
    if (
amount 0) {
        for (
int iamounti++) {
            list.
Push(0);
            list.
SwapAt(0, list.Length-1);
            list.
Erase(0);
        }
    }
    else { 
//amount is positive
        
for (int iamounti++) {
            list.
ShiftUp(0);
            list.
SwapAt(0, list.Length-1);
            list.
Erase(list.Length-1);
        }
    }

__________________

Last edited by ddhoward; 09-14-2016 at 01:44.
ddhoward is offline