cft

Reverse An Array - 3 Methods

Three Ways To Reverse An Array


user

Amritanshu Dev Rawat

a year ago | 1 min read

- Method 1 - Revere with For Loop and print


```java

public class ReverseArray {

public static void main(String[] args) {

// Array with some element

int[] myArray = {10,22,33,11,88,9,2};

// reverse loop and printing

for(int i = myArray.length-1;i>=0;i--) {

System.out.print(myArray[i]+ " ");

}

}

}

```


- Method 2 - Using Array List


```java

// Method 2 Reverse an array using array list

public class ReverseArray {

public static void main(String[] args) {

// Array List

ArrayList<Integer> myArray = new ArrayList<Integer>();

myArray.add(10);

myArray.add(21);

myArray.add(13);

myArray.add(14);

myArray.add(51);

// reverse array and method

Collections.reverse(myArray);

System.out.println(myArray);

}

}

```


- Method 3 - Two Pointers and Swap


```java

// Method 3 Reverse with swap

public class ReverseArray {

public static void main(String[] args) {


// Array with some element

int[] myArray = {10,22,33,11,88,9,2};


int start = 0;

int end = myArray.length-1;

int temp = 0;


// looping and swapping

while(end>start) {

temp = myArray[start];

myArray[start] = myArray[end];

myArray[end] = temp;

end--;

start++;

}


for(int i:myArray) {

System.out.print(i+" ");

}

}

}

```

Upvote


user
Created by

Amritanshu Dev Rawat

Developer


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles