-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse.java
More file actions
39 lines (32 loc) · 1.02 KB
/
Reverse.java
File metadata and controls
39 lines (32 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.mycompany.reverse;
/**
*
* @author ERIC
*/
public class Reverse {
public static void main(String[] args) {
// Input array
int[] array = {3, 4, 6, 1, 9, 7, 5, 8};
// Print original array
System.out.print("Original Array: ");
printArray(array);
// Reverse the array using a for loop
for (int i = 0; i < array.length / 2; i++) {
// Swap elements
int temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = temp;
}
// Print the reversed array
System.out.print("Reversed Array: ");
printArray(array);
}
// Method to print the array using streams
public static void printArray(int[] array) {
// Using Java Streams to print the array
System.out.println(java.util.Arrays.toString(array));
}
}