Sei sulla pagina 1di 4

8/20/2014 4 Ways to Reverse an Array in Java | JAVAbyNATARAJ

We can reverse an array in 4 different ways.


They are:

1. Reading array values from the last index

2. Rotate half of its array length and swap values

3. Use org.apache.commons.lang.ArrayUtils reverse method

4. Use common swapping technique.

The first way to reverse an array:


1. Reading array values from the last index
If the Array length is 6, then read the array from the index 5 to print from the reverse. This is
basic approach to print reverse of an array.

1 // First method to reverse an array ?


2 int arrnum[] = new int[] { 12, 10, 100, 67, 90 };
3 // print the original array
4 System.out.println("printing original array");
5 for (int i = 0; i < arrnum.length; i++) {
6 System.out.println(arrnum[i]);
7 }
8 // Reading array values from the last index
9 System.out.println("printing Reversed Array: ");
10 for (int i = arrnum.length - 1; i >= 0; i--) {
11 System.out.println(arrnum[i]);
12 }

Output:

90,67,100,10,12

2. Rotate half of its array length and swap values


The loop has to be rotated half of the length of the array times. For instance, if the length of
the array is 4 then the loop has to be rotated 2 times, if 11 then 5 times (you get 5.5 but
rounded to 5).

1 // Second method to reverse an array ?


2 int arr[] = new int[] { 10, 20, 30, 50, 70,80 };
3 System.out.println("Third method to reverse an array:");
4 for (int i = 0; i < arr.length / 2; i++) {
5 int temp = arr[i];
6 arr[i] = arr[arr.length - i - 1];
7 arr[arr.length - i - 1] = temp;
8 }
9 for (int i = 0; i < arr.length; i++) {
10 System.out.println(arr[i]);
11 }
http://javabynataraj.blogspot.in/2014/08/4-ways-to-reverse-array-in-java.html#more 1/4
8/20/2014 4 Ways to Reverse an Array in Java | JAVAbyNATARAJ

Output:

80,70,50,30,20,10

3. Use org.apache.commons.lang.ArrayUtils reverse method


This is Licensed to the Apache Software Foundation not from
Oracle.ArrayUtils.reverse(array) reverses the order of the given array.

1 // Third method to reverse an array ?


2 int array[] = new int[] { 11, 13, 24, 36, 44, 66 };
3 //using org.apache.commons.lang.ArrayUtils
4 ArrayUtils.reverse(array);
5 System.out.println("Using apache commons:" + array);
6 for (int i = 0; i < array.length; i++) {
7 System.out.println(array[i]);
8 }

Output:

66,44,36,24,13,11

4. Using common swapping technique.


This is common swapping technique to reversing array elements

1 // Fourth method to reverse an Array ?


2 int data[] = new int[] { 10,15, 25, 35, 45, 55 };
3 System.out.println("Fourth method to reverse an array:");
4 int left = 0;
5 int right = data.length - 1;
6 while (left < right) {
7 // swap the values at the left and right indices
8 int temp = data[left];
9 data[left] = data[right];
10 data[right] = temp;
11 // move the left and right index pointers in toward the center
12 left++;
13 right--;
14 }
15 for (int i = 0; i < data.length; i++) {
16 System.out.println(data[i]);
17 }

Output:

55,45,35,25,15,10

http://javabynataraj.blogspot.in/2014/08/4-ways-to-reverse-array-in-java.html#more 2/4
8/20/2014 4 Ways to Reverse an Array in Java | JAVAbyNATARAJ

Combining all the logics in a single program :

1 package com.javabynataraj; ?
2 import org.apache.commons.lang.ArrayUtils;
3 //http://javabynataraj.blogspot.com
4 public class ReverseArray {
5 public static void main(String[] args) {
6
7 int arrnum[] = new int[] { 12, 10, 100, 67, 90 };
8 // print the original array
9 System.out.println("printing original array");
10 for (int i = 0; i < arrnum.length; i++) {
11 System.out.println(arrnum[i]);
12 }
13 // Reading array values from the last index
14 System.out.println("printing Reversed Array: ");
15 for (int i = arrnum.length - 1; i >= 0; i--) {
16 System.out.println(arrnum[i]);
17
}
18
19
// Second method to reverse an array
20
int arr[] = new int[] { 10, 20, 30, 50, 70 };
21
22 System.out.println("Second method:");
23 System.out.println("Rotate half of its array length and swap values"
for (int i = 0; i < arr.length / 2; i++) {
24
25 int temp = arr[i];
26 arr[i] = arr[arr.length - i - 1];
27 arr[arr.length - i - 1] = temp;
28 }
29 for (int i = 0; i < arr.length; i++) {
30 System.out.println(arr[i]);
31 }
32
33 // Third method to reverse an array
34 int array[] = new int[] { 11, 13, 24, 36, 44, 66 };
35 System.out.println("Third method:Using apache commons");
36 //using org.apache.commons.lang.ArrayUtils
37 ArrayUtils.reverse(array);
38 for (int i = 0; i < array.length; i++) {
39 System.out.println(array[i]);
40 }
41
42 // Fourth method to reverse an Array
43 int data[] = new int[] { 15, 25, 35, 45, 55 };
44 System.out.println("Fourth method: swaping technique:");
45 int left = 0;
46 int right = data.length - 1;
47 while (left < right) {
48 // swap the values at the left and right indices
49 int temp = data[left];
50 data[left] = data[right];
51 data[right] = temp;
52 // move the left and right index pointers in toward the center
53 left++;
54 right--;
55 }
http://javabynataraj.blogspot.in/2014/08/4-ways-to-reverse-array-in-java.html#more 3/4
8/20/2014 4 Ways to Reverse an Array in Java | JAVAbyNATARAJ

56 for (int i = 0; i < data.length; i++) {


57 System.out.println(data[i]);
58 }
59 }
}

Run the above program and find the output.


please write your valuable comments if you find any bugs.

http://javabynataraj.blogspot.in/2014/08/4-ways-to-reverse-array-in-java.html#more 4/4

Potrebbero piacerti anche