1//java program to reverse array using for loop
2public class ReverseArrayDemo
3{
4 public static void main(String[] args)
5 {
6 int[] arrNumbers = new int[]{2, 4, 6, 8, 10};
7 System.out.println("Given array: ");
8 for(int a = 0; a < arrNumbers.length; a++)
9 {
10 System.out.print(arrNumbers[a] + " ");
11 }
12 System.out.println("Reverse array: ");
13 // looping array in reverse order
14 for(int a = arrNumbers.length - 1; a >= 0; a--)
15 {
16 System.out.print(arrNumbers[a] + " ");
17 }
18 }
19}
1// reverse a string using ByteArray
2class ReverseStringByteArray
3{
4 public static void main(String[] args)
5 {
6 String input = "HelloWorld";
7 // getBytes() method to convert string into bytes[].
8 byte[] strByteArray = input.getBytes();
9 byte[] output = new byte[strByteArray.length];
10 // store output in reverse order
11 for(int a = 0; a < strByteArray.length; a++)
12 output[a] = strByteArray[strByteArray.length - a - 1];
13 System.out.println(new String(output));
14 }
15}
1import java.util.Arrays;
2public class ReverseStringArrayInJava
3{
4 public static void main(String[] args)
5 {
6 String[] strHierarchy = new String[]{"Junior Developer","Senior Developer","Team Lead","Project Manager","Senior Manager","CEO"};
7 System.out.println("Given string array: " + Arrays.toString(strHierarchy));
8 for(int a = 0; a < strHierarchy.length / 2; a++)
9 {
10 String strTemp = strHierarchy[a];
11 strHierarchy[a] = strHierarchy[strHierarchy.length - a - 1];
12 strHierarchy[strHierarchy.length - a - 1] = strTemp;
13 }
14 System.out.println("Reversed string array: ");
15 for(int a = 0; a < strHierarchy.length; a++)
16 {
17 System.out.println(strHierarchy[a]);
18 }
19 }
20}