StringBuilder::reverse
Simply iterate the array, and replace each entry with its reverse. You can use a StringBuilder to reverse a String, calling StringBuilder.reverse.
Like,
public static void reverse(String[] array) {
for (int i = 0; i < array.length; i++) {
array[i] = new StringBuilder(array[i]).reverse().toString();
}
}
And then to test it
public static void main(String[] args) {
String arr[] = { "abc", "def" };
reverse(arr);
System.out.println(Arrays.toString(arr));
}
See this code run live at IdeOne.com.
Answer from Elliott Frisch on Stack Overflow[cba, fed]
Videos
How do I reverse an array in Java 8 using streams?
What is the best way to reverse an array in Java?
Does Java provide a built-in method to reverse an array?
StringBuilder::reverse
Simply iterate the array, and replace each entry with its reverse. You can use a StringBuilder to reverse a String, calling StringBuilder.reverse.
Like,
public static void reverse(String[] array) {
for (int i = 0; i < array.length; i++) {
array[i] = new StringBuilder(array[i]).reverse().toString();
}
}
And then to test it
public static void main(String[] args) {
String arr[] = { "abc", "def" };
reverse(arr);
System.out.println(Arrays.toString(arr));
}
See this code run live at IdeOne.com.
[cba, fed]
Stream
The Answer by Elliott Frisch is correct and robust, and should be accepted.
In addition, for fun, here is a version of that code using streams rather than the conventional for loop. I am not claiming this is better.
I do not know of a way for a stream of an array to affect that array. So instead here I make and return a fresh array.
public static String[] reverse( String[] array ) {
Objects.requireNonNull( array , "Received null argument where an array of `String` was expected. Message # b5c03336-4b9e-4735-a054-16e43aac059e.") ;
Stream< String > stream = Arrays.stream( array ) ;
String[] result =
stream
.map( ( String s ) -> new StringBuilder( s ).reverse().toString() )
.toArray(String[]::new)
;
return result ;
}
Usage.
String arr[] = { "abc" , "def" , "mask" } ;
String arr2[] = Ideone.reverse( arr ) ;
System.out.println( Arrays.toString( arr ) ) ;
System.out.println( Arrays.toString( arr2 ) ) ;
See that code run live at IdeOne.com.
[abc, def, mask]
[cba, fed, ksam]
The main issue with your way of doing it is your taking the n'th character from str and appending it to make it the n'th character of res.
You could fix it like this:
public String reverseString(String str) {
String res = "";
for (int i = str.length() - 1; i >= 0; i--) {
res = res + str.charAt(i);
}
return res;
}
You have your concatenation backwards. Try:
public String reverseString(String str) {
String res = "";
for (int i = 0; i < str.length(); i++) {
res = str.charAt(i) + res; // Add each char to the *front*
}
return res;
}
Note also the simpler, canonical, loop termination condition.
try this
Collections.reverse(Arrays.asList(reverse));
If you want to reverse the array you have stop your iterations in the middle of the array. Otherwise you reverse it twice and therefore it remains the same. Change your for loop to:
for ( int j = 0; j < array.length/2 ; j++){
Unfortunately I do not understand why do you need 2 loops. Just to print the initial array?
Try these steps:
String[] strDays = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday"};
List<String> list = Arrays.asList(strDays);
Collections.reverse(list);
strDays = (String[]) list.toArray();
This line looks wrong:
for (int j = s[i].length()-1; i >= 0; i--)
It should be:
for (int j = s[i].length()-1; j >= 0; j--)
In other words: the indexes in the inner loop are mistaken, they should be using j instead of i. As a side comment - here's a simpler way to reverse a string:
reverse = new StringBuilder(s[i]).reverse().toString();