0

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;
                }
            }

        }

    }
}
4
  • Hint: you don't need two loops, just one that cycles over the first half of the array. The rest is just some arithmetic for the array indexes. Commented Sep 22, 2021 at 9:27
  • @FedericoklezCulloca I Understnad, but why isn't my code working? Commented Sep 22, 2021 at 9:41
  • 1
    Note: you should essentially never invoke System.exit(), especially in a method that is just meant to reverse an array. If you want the method to stop, use return (or throw an exception, if you want to stop because of a problem). Commented Sep 22, 2021 at 9:44
  • 1
    @AbhinavK "but why isn't my code working" step through your code with a debugger; or, at the very least, print out arr regularly, so you can see how it's being changed. Commented Sep 22, 2021 at 9:45

5 Answers 5

5

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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));
  } 

5 Comments

Did you actually try this? If you didn't, give it a go.
yes i try this and it works!
It certainly compiles and runs; it just doesn't reverse arr. Check the link in my previous comment. Arrays.asList(arr) is List<int[]> with 1 element, not a List<Integer> with n elements.
Yes i changed the parameter. look here
Yes, that allows you to reverse an Integer[]; but OP has an int[], and so can't invoke this method.
0

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.

Comments

0

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]);
}

Comments

-1
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]);
        }

    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.