SHARE
TWEET

Untitled

advictoriam Jan 25th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Swapper
  2. {
  3.    /**
  4.       This method swaps adjacent elements of the given array
  5.       @param values an array
  6.    */
  7.    
  8.    public void swap(int[] values, int first, int second)
  9.    {
  10.       int temp = values[first];
  11.       values[first] = values[second];
  12.       values[second] = temp;
  13.    }
  14.    
  15.    public void swapAdjacentElements(int[] values)
  16.    {
  17.       for(int i = 0; i < values.length; i++)
  18.       {
  19.          swap(values, i, ++i);
  20.          if(i + 2 > values.length - 1){break;}
  21.       }
  22.    }
  23.    
  24.    
  25.    // This method is used to check your work
  26.    public int[] check(int[] values)
  27.    {
  28.       swapAdjacentElements(values);
  29.       return values;
  30.    }
  31. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top