Question
I got a multidimensional array of strings that is sent from jsp when I try to get the array and set it to a variable in the servlet. I understand that the controller converts the multidimensional rows of the array into a one-dimensional array of strings, I printed the array in console.log from the jsp side to make sure that the array contains arrays of strings, not just strings, and the chrome log showed that everything is in order, therefore I think the problem should be in the servlet side. This is not a mistake, but it is not the behavior that I expected from what I want to achieve in my application.
Here is the code I come across:
String[] arrayCompra = request.getParameterValues("arraycompra[]");
So basically what the servlet does when I install the array from the request to the one on the servlet side is something like this:
String[][] array = { {"a","b","c"} , {"d","e","f"} };
In it:
String[] array = {"a,b,c", "d,e,f"};
In addition, I searched for a solution and got some answers and ideas, such as adding .clone (); but it gave me the same result. And I know that there is a string.Split method, but I will need to iterate through the array to separate each row, and then set each resulting array to a multidimensional array of strings, and I find it unnecessary if I can avoid or solve the main problem.
What am I doing wrong? Is there any way to achieve what I want? is there a better way to make an exact copy of the multidimensional array that I get from a request to a servlet?
Thank you very much in advance.
Czech solution
String[][] arrayCompra = Arrays.stream(request.getParameterValues("arraycompra[]")).map(s -> s.split(",")).toArray(String[][]::new);
Btw... JDK 8 , 1,7, ...
lambda expressions are not supported in -source 1.7
(use -source 8 or higher to enable lambda expressions)
, JDK 8, , .