reverse a string in java arr code example

Example 1: reverse string java

// Use StringBuilder for non-thread environment, it is faster
String string="whatever";
String reverse = new StringBuilder(string).reverse().toString();
System.out.println(reverse);

Example 2: Java reverse string array

// reverse string array in java using collections
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ReverseStringArrayUsingCollections
{
   public static void main(String[] args)
   {
      // creating list of strings
      List<String> li = new ArrayList<String>();
      li.add("java");
      li.add("core");
      li.add("world");
      li.add("hello");
      System.out.println("Given list: " + li);
      Collections.reverse(li);
      System.out.println("After using collections: " + li);
   }
}

Tags:

Java Example