Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4. import java.util.Arrays;
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9. inputArray();
  10. }
  11.  
  12. public static void inputArray() {
  13. Scanner scan = new Scanner(System.in);
  14.  
  15. System.out.println("Enter array size:");
  16. int arraySize = scan.nextInt();
  17. int[] array = new int[arraySize];
  18.  
  19. for (int i = 0; i < array.length; i++) {
  20. System.out.println("Enter array value " + (i+1));
  21. array[i] = scan.nextInt();
  22. }
  23.  
  24. reverse(array);
  25. }
  26.  
  27. public static void reverse(int[] array) {
  28. int[] tempArray = new int[array.length];
  29.  
  30. System.out.println("Array before reverse: " + Arrays.toString(array));
  31.  
  32. for (int i = 0; i < array.length; i++) {
  33. tempArray[i] = (array.length - i);
  34. }
  35.  
  36. array = tempArray;
  37. System.out.println("Array after reverse: " + Arrays.toString(tempArray));
  38. }
  39.  
  40. }
  41.  
  42. // write a method called reverse() with an int array as a parameter
  43. // The method should not return any value. The method can modify the array parameter
  44.  
  45. // In main() test the reverse() method and print the array both in reversed and non-reversed order
  46.  
  47. // To reverse an array, we need to swap the position of the elements
  48. // For example, an array of {1 5 9 9 2} would become {2 9 9 5 1}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement