In Java, you may convert a string to an array.
-
I have a string array that says "ABCD", "EFGH", "IJKL", and "MNOP". I'm reversing the array as well as individual strings inside the array, and I expect the result to be an array of strings, but the output is a string.
My code is shown below.public class ReverseString { String s = ""; public static void main(String[] args) { ReverseString rs = new ReverseString(); String value = ""; String arr[] = { "ABCD", "EFGH", "IJKL", "MNOP" }; for (int i = arr.length - 1; i >= 0; i--) { value = rs.reverseArray(arr[i]); } System.out.println(value); } public String reverseArray(String arr1) { for (int k = arr1.length() - 1; k >= 0; k--) { s += arr1.charAt(k); } return s.toString(); } }
and the result is PONMLKJIHGFEDCBA
An online site like this suggests saving the return of reverseArray rather than simply reporting it. Is that right? How do I convert it back to an array? -
@Sachin-Bhatt said in In Java, you may convert a string to an array.:
and I expect the result to be an array of strings, but the output is a string.
So this is a question about Java, not Qt, right? And you have example Java code but you'd like someone to comment on/modify it?
I have never used Java, but I think a 30 second glance at your code indicates it is printing a string because you go
System.out.println(value);
after the loop, and that will be the last value whichvalue
(aString
) happened to take in the loop?Surely you want to print out the reversed array you have just constructed,System.out.println(rs);
, don't you??EDIT
No, that's not right. Let me try again.I'm reversing the array as well
I expect the result to be an array of stringsI don't see that. Where do you think you are reversing (the elements in)
String arr[]
? You are iterating through them in reverse array order, but nothing stores those into either the originalarr[]
or any new array? That's what I see? To actually put the results into an array you would (presumably) need to save the returned result fromreverseArray()
into a (new) array as you go through the loop.and I expect the result to be an array of strings, but the output is a string.
I am confused. You only print
value
outside the loop, yet it contains all the characters from all the elements? You havevalue = rs.reverseArray(arr[i]);
inside the loop, you sure it is notvalue += rs.reverseArray(arr[i]);
? -
Non-static variable s caused incorrect accumulation of reversed characters. Overwriting value in the loop meant only the last reversed string was printed. Here is the corrected version reverses each string in the array in-place and prints the entire reversed array.
public class ReverseStringArray { public static void main(String[] args) { String[] array = {"ABCD", "EFGH", "IJKL", "MNOP"}; for (int i = 0; i < array.length / 2; i++) { String temp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = temp; } for (String str : array) { System.out.print(str + " "); } } }
You can find the more details on this link where I shared.