Answers for "reverse a string array in java"

3

reverse string java

String string="whatever";
String reverse = new StringBuffer(string).reverse().toString();
System.out.println(reverse);
Posted by: Guest on October-14-2020
2

java reverse a string

// Library approach
public static String solution(String str) {
	// StringBuilder is not thread-safe, use StringBuffer
	return new StringBuffer(str).reverse().toString();
}

// DIY approach
public static String solution(String str) {
	char[] chars = str.toCharArray();
	for(int i = 0, j = str.length() - 1; i < j; i++, j--) {
		char ch = chars[i];
		chars[i] = chars[j];
		chars[j] = ch;
	}
	return new String(chars);
}
Posted by: Guest on March-02-2021
2

java string reverse

String str = "Reverse this strings";
//for loop
for (int i = str.length() - 1; i >= 0; i--) {
	System.out.print(str.charAt(i));
}

// StringBuffer for multi thread
StringBuffer sb = new StringBuffer(); 
sb.append(str);
System.out.print(sb.reverse().toString());

// StringBuffer for single thread
StringBuilder sb = new StringBuilder(); 
sb.append(str);
System.out.print(sb.reverse().toString());
Posted by: Guest on June-01-2020
0

reverse a string in java

public class StringReverseExample{
   public static void main(String[] args) {
      String string = "abcdef";
      String reverse = new StringBuffer(string).reverse().toString();
      System.out.println("\nString before reverse: "+string);
      System.out.println("String after reverse: "+reverse);
   }
}
Posted by: Guest on August-20-2021
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);
   }
}
Posted by: Guest on December-10-2020
2

reverse string array java

//java program to reverse array using for loop
public class ReverseArrayDemo 
{
   public static void main(String[] args) 
   {
      int[] arrNumbers = new int[]{2, 4, 6, 8, 10};  
      System.out.println("Given array: ");  
      for(int a = 0; a < arrNumbers.length; a++)
      {
         System.out.print(arrNumbers[a] + " ");
      }
      System.out.println("Reverse array: ");
      // looping array in reverse order
      for(int a = arrNumbers.length - 1; a >= 0; a--) 
      {  
         System.out.print(arrNumbers[a] + " ");  
      }
   }
}
Posted by: Guest on November-07-2020

Code answers related to "reverse a string array in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language