Reverse An Array – 3 Methods

Method 1 – Revere with For Loop and print

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>…


This content originally appeared on DEV Community and was authored by Amritanshu Dev Rawat

  • Method 1 - Revere with For Loop and print
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
// 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
// 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+" ");
    }
}
}


This content originally appeared on DEV Community and was authored by Amritanshu Dev Rawat


Print Share Comment Cite Upload Translate Updates
APA

Amritanshu Dev Rawat | Sciencx (2021-06-08T17:54:47+00:00) Reverse An Array – 3 Methods. Retrieved from https://www.scien.cx/2021/06/08/reverse-an-array-3-methods/

MLA
" » Reverse An Array – 3 Methods." Amritanshu Dev Rawat | Sciencx - Tuesday June 8, 2021, https://www.scien.cx/2021/06/08/reverse-an-array-3-methods/
HARVARD
Amritanshu Dev Rawat | Sciencx Tuesday June 8, 2021 » Reverse An Array – 3 Methods., viewed ,<https://www.scien.cx/2021/06/08/reverse-an-array-3-methods/>
VANCOUVER
Amritanshu Dev Rawat | Sciencx - » Reverse An Array – 3 Methods. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/08/reverse-an-array-3-methods/
CHICAGO
" » Reverse An Array – 3 Methods." Amritanshu Dev Rawat | Sciencx - Accessed . https://www.scien.cx/2021/06/08/reverse-an-array-3-methods/
IEEE
" » Reverse An Array – 3 Methods." Amritanshu Dev Rawat | Sciencx [Online]. Available: https://www.scien.cx/2021/06/08/reverse-an-array-3-methods/. [Accessed: ]
rf:citation
» Reverse An Array – 3 Methods | Amritanshu Dev Rawat | Sciencx | https://www.scien.cx/2021/06/08/reverse-an-array-3-methods/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.