1int length = array.length;
2 for(int i=0;i<length/2;i++) {
3 int swap = array[i];
4 array[i] = array[length-i-1];
5 array[length-i-1] = swap;
6 }
7or
8Collections.reverse(Arrays.asList(array));
1public class ReverseString {
2 public static void main(String[] args) {
3 String s1 = "neelendra";
4 for(int i=s1.length()-1;i>=0;i--)
5 {
6 System.out.print(s1.charAt(i));
7 }
8 }
9}
1String string="whatever";
2String reverse = new StringBuffer(string).reverse().toString();
3System.out.println(reverse);
4
1// reverse a string using reverse() method of StringBuilder class
2class ReverseUsingReverseMethod
3{
4 public static void main(String[] args)
5 {
6 String str = "Hello world Java";
7 StringBuilder sb = new StringBuilder();
8 // append string to StringBuilder
9 sb.append(str);
10 sb = sb.reverse();
11 // printing reversed String
12 System.out.println(sb);
13 }
14}
1// reverse string array in java using collections
2import java.util.ArrayList;
3import java.util.Collections;
4import java.util.List;
5public class ReverseStringArrayUsingCollections
6{
7 public static void main(String[] args)
8 {
9 // creating list of strings
10 List<String> li = new ArrayList<String>();
11 li.add("java");
12 li.add("core");
13 li.add("world");
14 li.add("hello");
15 System.out.println("Given list: " + li);
16 Collections.reverse(li);
17 System.out.println("After using collections: " + li);
18 }
19}