Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Ex1 {
- // Write a Java program to reverse an array of integer values
- public static void main(String[] args) {
- // initialize array
- int[] arr = { 3, 1, 5, 2, 7, 12, 79, 10 };
- // print source array
- System.out.print("Array before reverse: ");
- for (int i = 0; i < arr.length; i += 1) {
- System.out.print(arr[i] + " ");
- }
- System.out.println();
- // reverse the array order
- int left = 0;
- int right = arr.length - 1;
- while (left < right) {
- // switch values
- int tmp = arr[left];
- arr[left] = arr[right];
- arr[right] = tmp;
- left += 1;
- right -= 1;
- }
- // print the array after reverse
- System.out.print("Array after reverse : ");
- for (int i = 0; i < arr.length; i += 1) {
- System.out.print(arr[i] + " ");
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement