In Java, you may convert a string to an array.
-
wrote on 1 Dec 2022, 09:14 last edited by
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? -
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?wrote on 1 Dec 2022, 09:21 last edited by JonB 12 Jan 2022, 15:09@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]);
? -
wrote on 1 Dec 2022, 14:05 last edited by
You define your
value
as a string, which is where the result is collected.
Don't want a string, fix the software. -
This post is deleted!