/ Published in: Java
3 ways to reverse array in Java
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import java.util.Arrays; public class Practive{ { int [] array = new int[] {23,45,34,54,67,78}; //reverse method 1: int i =0; while ( i< (array.length)/2){ for (int j = array.length-1; j>i;j--){ int tempf = array[i]; int tempb = array[j]; array[i] = tempb; array[j]= tempf; i++; } } } } /* //reverse method 2: import java.util.Arrays; public class Practive{ public static void main (String[] args) { int [] array = new int[] {23,45,34,54,67,78}; System.out.println("Original array:"); System.out.println(Arrays.toString(array)); //reverse for (int i =0; i< (array.length)/2; i++){ for (int j = array.length-1; j>i;j--){ int tempf = array[i]; int tempb = array[j]; array[i] = tempb; array[j]= tempf; i++; System.out.println("Reverse array"); System.out.println(Arrays.toString(array)); } } } } */ /* import java.util.Arrays; public class Practive{ public static void main (String[] args) { int [] array = new int[] {23,45,34,54,67,78}; System.out.println("Original array:"); System.out.println(Arrays.toString(array)); //reverse method 3 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; System.out.println("Reverse array"); System.out.println(Arrays.toString(array)); } } } */