fork download
  1. import java.util.Random;
  2.  
  3. class Main { // Online compilers often require 'Main' as the class name
  4. public static void main(String[] args) {
  5. int[] Data = new int[5];
  6.  
  7. // Generate random numbers (0-9) for each element
  8. for (int i = 0; i < Data.length; i++) {
  9. Data[i] = (int) (Math.random() * 10);
  10. }
  11.  
  12. System.out.println("Original Array:");
  13. printContents(Data);
  14.  
  15. // Reverse the array
  16. int[] Backwards = Reverse(Data);
  17.  
  18. System.out.println("Reversed Array:");
  19. printContents(Backwards);
  20. }
  21.  
  22. // Method to print elements of the array
  23. public static void printContents(int[] arr) {
  24. for (int num : arr) {
  25. System.out.print(num + " ");
  26. }
  27. System.out.println();
  28. }
  29.  
  30. // Method to reverse the array and return the new reversed array
  31. public static int[] Reverse(int[] arr) {
  32. int[] Backwards = new int[arr.length];
  33.  
  34. for (int i = 0; i < arr.length; i++) {
  35. Backwards[i] = arr[arr.length - 1 - i]; // Copy elements in reverse order
  36. }
  37.  
  38. return Backwards;
  39. }
  40. }
  41.  
  42.  
Success #stdin #stdout 0.12s 55688KB
stdin
Standard input is empty
stdout
Original Array:
7 7 5 4 3 
Reversed Array:
3 4 5 7 7