Swap array elements
Monkey Forums/Monkey Programming/Swap array elements
| ||
What is the best solution to swap array elements? |
| ||
Local temp:=arr[10] arr[10] = arr[15] arr[15] = temp |
| ||
Samah, Thanks, but how can I do this with a big array? I want to swap each 5th element of an array with each 8th element. Here's what I'm trying to do: For Local a:Int=0 Until arr.Length Step 5 Local temp:=arr[a] arr[a]=arr[a+3] arr[a+3]=temp Next But it throws an error: Array index out of range |
| ||
How big is your array? |
| ||
when a reaches arr.Length it tries to get to arr[a+3] which is out of array. maybe check like: For Local a:Int=0 Until arr.Length Step 5 if a+3 < arr.Length Local temp:=arr[a] arr[a]=arr[a+3] arr[a+3]=temp endif Next |
| ||
Samah, Array length always changes. Duke87, Thanks for the tip, it's working now. |