StefanTobler

Lesson 33 Activity 4

Dec 3rd, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. /*
  2.  * Lesson 33 Coding Activity 4
  3.  *
  4. Write a method that takes an array of ints and reverses the order of the values in the array. So the array {1, 2, 3} would be changed to {3, 2, 1}.
  5.  
  6. This method must be called reverse()and it must take an int[]parameter.
  7.  *
  8.  */
  9.  
  10.  
  11. import java.util.Scanner;
  12.  
  13. class Lesson_33_Activity_Four {
  14.  
  15.     public static void reverse(int x[])
  16.     {
  17.       int z[] = new int[x.length];
  18.       for (int i = 0; i < x.length; i++)
  19.       {
  20.         z[i] = x[i];
  21.       }
  22.       for (int i = 0; i < x.length; i++)
  23.       {
  24.         x[i] = z[z.length-(i+1)];
  25.       }
  26.     }
  27.  
  28.     public static void main(String[] args)
  29.      {
  30.       int x[] = {1,2,3};
  31.       reverse(x);
  32.       for (int i = 0; i < x.length; i++)
  33.        System.out.print(x[i] + " ");
  34.     }
  35. }
Add Comment
Please, Sign In to add comment