Home > other >  Trying to reverse array elements
Trying to reverse array elements

Time:09-22

I am trying to sort the array elements in a reverse order, but it doesn't seem to give me correct output. i have tried many times to correct it but not able to do so. I am a complete beginner. any help?

CODE

import java.util.Arrays;
import java.util.Scanner;

public class main {

    public static void main(String[] args) {
        int[] arr = new int[5];
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the array Elements");
        for (int i = 0; i < 5; i  ) {
            arr[i] = input.nextInt();
        }
        swap(arr);
        input.close();
    }

    static void swap(int arr[]) {
        for (int start = 0; start < 5; start  ) {
            for (int end = arr.length - 1; end >= 0; end--) {
                if (start >= end) {
                    System.out.println(Arrays.toString(arr));
                    System.exit(0);
                } else {
                    int temp = arr[start];
                    arr[start] = arr[end];
                    arr[end] = temp;
                }
            }

        }

    }
}

CodePudding user response:

If you want to reverse the elements in the array, you need to write method swap() like this:

static void swap(int[] arr) {
    for (int start = 0; start < arr.length / 2; start  ) {
        int tmp = arr[arr.length - 1 - start];
        arr[arr.length - 1 - start] = arr[start];
        arr[start] = tmp;
    }
}

CodePudding user response:

import java.util.Arrays; 
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
         int[] arr = new int[5];
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the array Elements");
        for (int i = 0; i < 5; i  ) {
            arr[i] = input.nextInt();
        }
        swap(arr,arr.length  );
        input.close();
    }
    
     static void swap(int a[], int n) {
         int i, k, t;
        for (i = 0; i < n / 2; i  ) {
            t = a[i];
            a[i] = a[n - i - 1];
            a[n - i - 1] = t;
        }
 
        /*printing the reversed array*/
        System.out.println("Reversed array is: \n");
        for (k = 0; k < n; k  ) {
            System.out.println(a[k]);
        }

    }

}

CodePudding user response:

You can use the ‘reverse’ method present in the collections framework. But for this, you first need to convert an array to a list as the ‘reverse’ method takes the list as an argument.

static void swap(Integer arr[]) { 
     Collections.reverse(Arrays.asList(arr)); 
     System.out.println(Arrays.toString(myArray));
  } 

CodePudding user response:

You can use ArrayUtils#reverse https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/ArrayUtils.html#reverse-int:A- To avoid the creation of List and to reverse the array in-place.

CodePudding user response:

Personally, I would use the Collections method, transforming your array into a List, sorting it with the reverse() method and then transforming it back to an array, like so:

public void invert(int[] array) {
    List<int> list = Arrays.asList(array);
    Collections.reverse(list);
    array = list.toArray(new int[0]);
}
  •  Tags:  
  • java
  • Related