Skip to content

[FEATURE REQUEST] <Reverse an array> #4472

@sanvi-05

Description

@sanvi-05

What would you like to Propose?

I propose adding a new feature to the project to reverse an array. This enhancement will provide a useful utility for developers who need to reverse the order of elements in an array.

Title: Reverse an Array

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 reverseArray method to the project to simplify array reversal and improve code readability.

Issue details

Code :

import java.util.*;
public class Main{
public static void reverseArray(int[] arr, int start, int end){
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;
reverseArray(arr,start,end);
}
}`

Description:
reverseArray 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 case 1:
Input :
Enter the array size 5
Enter array elements 7 2 9 0 1
Output :
Reversed array :
1
0
9
2
7

Test case 2:
Input :
Enter the array size 7
Enter array elements 5 2 0 6 8 2 6
Output :
Reversed array :
6
2
8
6
0
2
5

Additional Information

No response

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