swap two element in int array - Java java.lang

Java examples for java.lang:int Array

Description

swap two element in int array

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int[] arr = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        int i = 2;
        int j = 2;
        System.out.println(java.util.Arrays.toString(swap(arr, i, j)));
    }// w w  w .  j a va 2  s .c o  m

    /**
     * swap two element in array
     */
    private static int[] swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        return arr;
    }
}

Related Tutorials