Try these steps:
String[] strDays = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday"};
List<String> list = Arrays.asList(strDays);
Collections.reverse(list);
strDays = (String[]) list.toArray();
Answer from Vijay on Stack OverflowTry 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();
Videos
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]
When I run your code, I didn't get the same error that you posted, but I did notice that null was at the end of each reversed word.
nullyadnoM
nullyadseuT
nullyadsendeW
Which is beacuse when you create a new string array, all it's values default to null:
String[] t = new String[words.length];
The easiest way to fix it is to set it's value to an empty string, before you start adding to it:
public static String[] reverseString(String[] words)
{
String[] text = new String[words.length];
for (int i = 0; i < words.length; i++)
{
text[i] = "";
for (int j = words[i].length() - 1; j >= 0; j--)
text[i] += words[i].charAt(j);
}
return text;
}
I have tested this code, and it works perfectly fine for me.
To output the array, instead of using
System.out.println(words);
use the following:
System.out.println(Arrays.toString(words));
This will give you the output:
[yadnoM, yadseuT, yadsendeW]
You can transform your string into StringBuilder and it had reverse method. And its always better to use foreach rather than for, until there is actual need.
public String[] reverseString(String[] words) {
String[] t = new String[words.length];
for (String wordTemp : words) {
StringBuilder sb = new StringBuilder(wordTemp);
t[i] = sb.reverse().toString();
}
return t;
}
Alternate approach :-
public String[] reverseString(String[] words)
{
String[] t=new String[words.length];
for(int i=0;i<words.length;i++)
{
//added for setting elemennt as emptyString instead of null
t[i] = "";
for(int j=words[i].length()-1;j>=0;j--)
{
t[i]+=words[i].substring(j,j+1);
}
}
//using loop
for(int i=0;i<words.length;i++)
{
System.out.println(t[i]);
}
//using Arrays Method
System.out.println(Arrays.toString(t));
return t;
}