java

ReverseArray()

Parameters: int[] arr

An integer array 'arr' which needs to be reversed

Returns: Array 'arr' with elements in reverse order

This function takes an array and returns a new array with elements in reverse order. It applies basic programming principles such as variables, loops and array length.

variables
loops
arrays
array length
Medium dificulty

Creating a Function to Reverse an Array in Java

Hello, Programmer! Welcome to this blog post. We are going to guide you in a hassle-free way on how to program a 'reverse-array' function in java. No worries, we'll bypass any difficult jargon. With a clear mind and keen focus, let's dive straight into it. Keep in mind, each problem in programming is a step towards your enhancement.

Step 1: Create an Array

The first step is to create an array. You can initialize and populate the array with elements using either array literal or array constructor. Here, we are using an array literal.

Integer[] array = {1, 2, 3, 4, 5};

Step 2: Print Original Array

Printing the original array before reversing it is not necessary, but it helps to verify the final output. We use a for-each loop to traverse the array and print each element.

for(Integer i : array) {
    System.out.print(i + " ");
}
System.out.println();

Step 3: Reverse the Array

To reverse the array, we will create a function called reverseArray. In this function, we'll use a for loop running from the first index to the middle of the array. On each iteration, we'll swap the elements at index i and array.length - 1 - i.

public static void reverseArray(Integer[] array) {
    for(int i = 0; i < array.length / 2; i++) {
        int temp = array[i];
        array[i] = array[array.length - 1 - i];
        array[array.length - 1 - i] = temp;
    }
}

Step 4: Call the Reverse Function

Next, we will call the reverseArray function and pass our array to it.

reverseArray(array);

Step 5: Print Reversed Array

Lastly, we will use another for-each loop similar to step 2 to print the reversed array.

for(Integer i : array) {
    System.out.print(i + " ");
}

Conclusion

The whole code for reversing an array in Java would look something like this:

public class Main {
    public static void main(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5};

        for(Integer i : array) {
            System.out.print(i + " ");
        }
        System.out.println();

        reverseArray(array);

        for(Integer i : array) {
            System.out.print(i + " ");
        }
    }

    public static void reverseArray(Integer[] array) {
        for(int i = 0; i < array.length / 2; i++) {
            int temp = array[i];
            array[i] = array[array.length - 1 - i];
            array[array.length - 1 - i] = temp;
        }
    }
}

This code will print out the original array, reverse it, and then print out the reversed array. You can use this pattern to reverse any array you like.

Learn function in:

Array Reversal

The function reverses the order of elements in an array

Learn more

Mathematical principle

Using looping structure, function iterates over the array from the end to the start. For each iteration, it appends the current item to the result array. It's like flipping a list of numbers from `1,2,3,4,5` to `5,4,3,2,1`.

Learn more