Skip to content

[FEATURE REQUEST] <Reverse an Array> #4472 #4480

@Abdullahkhanspn

Description

@Abdullahkhanspn

What would you like to Propose?

I Propose new features to the project Reverse an Array
This modification will make the code easy to understand by the Users , Beginners , Developers . And provide a useful utility for developers who need to reverse the order of elements in an array.

**Title: Reverse an Array #4472 **
Problem statement : Java lacks a built-in method to reverse an array. This missing functionality complicates development tasks, such as sorting algorithms and data manipulation, and requires developers to write custom reversal logic. I propose adding a Reverse Array method to the project to simplify array reversal and improve code readability. So I make this Users Friendly.

**Here are my new Feature and Enhancement to the code **
1.Method Name
Consider using a more conventional method name such as reverse instead of reverseArray. This aligns better with common naming conventions in Java and makes it more intuitive for other developers.
2.Comments
Add comments or Javadoc comments to explain the purpose of the method, its parameters, and any important details about how it works. This will make the code more understandable for others who might use or maintain it.
3.Input Validation
Consider adding input validation to ensure that the start and end indices are within the bounds of the array to avoid potential ArrayIndexOutOfBoundsException.

Issue details

Code:
import java.util.*;

public class Main {
/**
* Reverses an array in-place.
*
* @param arr The array to be reversed.
* @param start The starting index for the reversal.
* @param end The ending index for the reversal.
*/
public static void reverse(int[] arr, int start, int end) {
if (arr == null || start < 0 || end >= arr.length || start >= end) {
// Handle invalid input gracefully
System.out.println("Invalid input for reversal.");
return;
}

    int temp;
    while (start < end) {
        temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }

    System.out.println("Reversed array:");
    for (int i = 0; i < arr.length; i++) {
        System.out.println(arr[i]);
    }
}

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the array size");
    int n = sc.nextInt();
    int[] arr = new int[n];
    System.out.println("Enter array elements");
    for (int i = 0; i < n; i++) {
        arr[i] = sc.nextInt();
    }
    int start = 0;
    int end = n - 1;
    reverse(arr, start, end);
}

}

Description
Imports: The program starts by importing the 'java.util' package, which is needed to use the 'Scanner' class for user input.
reverse Method: This method is defined to reverse the elements of an array. It takes three parameters:
arr: The array that you want to reverse.
start: The starting index for the reversal.
end: The ending index for the reversal.
Inside this method, a while loop is used to swap the elements at the start and end positions. It continues swapping elements until start is no longer less than end. This effectively reverses the array in-place.

Test Cases of the Upper Code
Test case 1:
Input :
Enter the array size 6
Enter array elements 2 4 6 7 2 4
Output :
Reversed array :
4
2
7
6
4
2

Test case 2:
Input :
Enter the array size 8
Enter array elements 4 3 1 7 8 2 9
Output :
Reversed array :
9
2
8
7
1
3
4

Additional Information

I Run this Code on my Locally setup.
array

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions