Advertisement
advictoriam

Untitled

Jan 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement