Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

swap in array

import java.util.Arrays;

public class swapInArray {

    public static void main (String[] args) {
                int[] arr = {1,10,100,1000};
                swap(arr,1,3);
                System.out.println(Arrays.toString(arr));
            }

            private static void swap (int[] arr, int index1, int index2){
                int elem1 = arr[index1];
                int elem2 = arr[index2];
                arr[index1] = elem1;
                arr[index2] = elem2;
    }
}
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #swap #array
ADD COMMENT
Topic
Name
7+6 =