-3

I am new to java. So can anyone help me out with the code? I have written the below code and need inputs. Can someone tell me what have i done wrong in the following code? I want to take inputs from the user and hence the first chunk of code. I dont want to pass an array with values in the main method.

I have written the following code: //Reverse an Array of integer values

package Test;
import java.util.*;

public class ArrayReverse {

    public static void main(String[] args) {
        int n;
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the numbers to be stored in an array: ");
        n = sc.nextInt();
        int a[] = new int[n];

        System.out.println("The numbers in array are: ");
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        System.out.println("Original Array is: "+Arrays.toString(a));
        for (int j = a.length-1; j >= 0; j--) {
            a[j] = sc.nextInt();
        }
                System.out.println("Reverse array is: "+Arrays.toString(a));
    }
}

Runtime21
  • 1
  • 1
  • Does this answer your question? [How do I reverse an int array in Java?](https://stackoverflow.com/questions/2137755/how-do-i-reverse-an-int-array-in-java) – AztecCodes Jul 25 '23 at 18:42

1 Answers1

0

To make the code simpler and also putting code reusability into consideration, you will require a method that reverses a given array. This method can be invoked any time in the main method. The method can return anything or be void. For my case i used the void method, it takes an array and performs the reverse reversing operation. In the method i used the two pointer. The pointers "start" and "end" are initially set to the array's first and last indices, respectively. The loop continues until finish is larger than or equal to start. The elements at the start and end indices are switched around at each iteration, and the pointers are moved in the direction of the array's centre. In the main method, using scanner, declare the size of the array and pass it to the array. Using a for loop pass the numbers you desire to the array i.e from the keyboard and map them to the indices of the array. Use another for loop to access the array(not reversed). Invoke the reverse array method and pass the array in the method. Use a for loop or for each loop to access the reversed array. for my case i used for each loop.

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

public class ReverseArray {
    public static void main(String[] args) {
        Scanner sn = new Scanner(System.in);
        //size of the array
        System.out.println("Enter the size of you array:");
        int size = sn.nextInt();
        //create the array of size(size)
        int[] numbers = new int[size];
        //create a for loop to ask user to input numbers at each index
        System.out.println("Enter " + size + " numbers.");
        for (int i = 0; i < size; i++) {
            numbers[i] = sn.nextInt();
        }
        //display the original array
        System.out.println("Original array:");
        for (int elements : numbers)
            System.out.print(elements + " ");
        //call the reverse method to reverse the numbers/ array
        reverseArray(numbers);
        //display the reversed array
        System.out.println();
        System.out.println("Reversed array: ");
        for (int nums : numbers) {
            System.out.print(nums + " ");
        }

        sn.close();

    }
    //method to reverse the array
    //it takes an array and performs the reversing operation
    public static void reverseArray(int[] array) {
        //starting point of the array...first index
        int start = 0;
        //endpoint or the last index
        int end = array.length - 1;

        while (start < end) {
            //create a temporary variable
            //this method is called the two pointers approach
            int temp = array[start];
            array[start] = array[end];
            array[end] = temp;

            //start increases while the  end decreases hence
            start++;
            end--;

        }

    }
}
Ian Dancan
  • 26
  • 5