---
# System prepended metadata

title: Java - 3 ways to reverse an array
tags: [Java, Basic Java]

---

# Java - 3 ways to reverse an array

###### tags: `Java` `Basic Java`

## Method 1: Setting Two Pointers (Start & End)(Fastest)
```
    public static void pointerSwap(int[] array) {
        int start = 0;
        int end = array.length - 1;
        int temp;
        while(start >= end) {
            temp = array[start];
            array[start] = array[end];
            array[end] = temp;
            start++;
            end--;
        }
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + "\t");
        }
    }
```
Point: while(start >= end)
= for odd elements array
\> for even elements array
**Be sure the start pointer does not exceed end pointer**

## Method 2: Element Swapping (Slower)
```
    public static void elementSwap(int[] array) {
        int temp;
        for (int i = 0; i < array.length / 2; i++) {
            temp = array[0 + i];
            array[0 + i] = array[array.length - 1 - i];
            array[array.length - 1 - i] = temp;
        }
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + "\t");
        }
    }
```

Point:

```
            temp = array[0 + i];
            array[0 + i] = array[array.length - 1 - i];
            array[array.length - 1 - i] = temp;
```
Explanation:
assume an array [index_0, index_1 , index_2, index_3, index_4, index_5]

where array.length = 6

we want: (3 swaps)
index_0 <-> index_5
index_1 <-> index_4
index_2 <-> index_3

which means, when i = 0; (3 swaps)
index_i     <-> index_(array.length - 1) - 0
index_i + 1 <-> index_(array.length - 1) - 1
index_i + 2 <-> index_(array.length - 1) - 2

so the iteration will be:
for (int i = 0; i < **array.length / 2**; i++)
**where array.length / 2 means how many swaps will be made** (in this case, 6 / 2 = 3)

## Method 3: Backwards Iteration (Slowest)
```
    public static int[] reverseArray(int[] array) {

        int[] returnArray = new int[array.length];

        int index = array.length - 1;

        for (int i = 0; i < array.length; i++) {
            returnArray[index] = array[i];
            index--;
        }
        return returnArray;
    }
```

## Complete Code:
```
public class Reverse {

    public static int[] input = {11, 22, 33, 44, 55};

    public static void main(String[] args) {

        int [] result = reverseArray(input);

        for (int i : result) {
            System.out.print(i + "\t");
        }
        System.out.println();

        elementSwap(input);

        System.out.println();

        pointerSwap(input);

    }

    public static void pointerSwap(int[] array) {
        int start = 0, 
        int end = array.length - 1;
        int temp;
        while(start >= end) {
            temp = array[start];
            array[start] = array[end];
            array[end] = temp;
            start++;
            end--;
        }
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + "\t");
        }
    }

    public static void elementSwap(int[] array) {
        int temp;
        for (int i = 0; i < array.length / 2; i++) {
            temp = array[0 + i];
            array[0 + i] = array[array.length - 1 - i];
            array[array.length - 1 - i] = temp;
        }
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + "\t");
        }
    }



    public static int[] reverseArray(int[] array) {

        int[] returnArray = new int[array.length];

        int index = array.length - 1;

        for (int i = 0; i < array.length; i++) {
            returnArray[index] = array[i];
            index--;
        }
        return returnArray;
    }
}

```

Output Result:
```
55	44	33	22	11	-> 6978400 ns

55	44	33	22	11	->  330200 ns

55	44	33	22	11	->  275600 ns

```

