[英]How to convert two dimensional array of string into one of type int?
How can I convert a double array of type String to a double array of type int?如何将 String 类型的双精度数组转换为 int 类型的双精度数组?
@PostMapping("/hole/coordinate")
@ResponseBody
public String saveCoordinate(@RequestBody Map<String, Object> params) {
System.out.println("params = " + params);
System.out.println("params = " + params.get("coordinate"));
return "success";
}
System.out.println(params.get("coordinate"));
store [[445, 292], [585, 331], [612, 223], [205, 532]]
There are m 2 elements of the double array. store
[[445, 292], [585, 331], [612, 223], [205, 532]]
double数组有m个2个元素。 ex) [a,b],[c,d].....m At this time, I want to receive the result in the data type of int[][], not String. ex) [a,b],[c,d].....m 这时候我想接收int[][]数据类型的结果,而不是String。
I was wondering how can I convert from String to int[][].我想知道如何从 String 转换为 int[][]。
I tried like below我试过如下
int[] arr= Stream.of(str.replaceAll("[\\[\\]\\, ]", "").split("")).mapToInt(Integer::parseInt).toArray();
for (int i : arr) {
System.out.println("i = " + i);
}
but it give me但它给我
4
4
5
2
9
2
...
Best Regards!最好的祝福!
You could parse it manually, as done in other answers, but since you are using spring
, you should use the tools it offers you.您可以像其他答案一样手动解析它,但由于您使用的是
spring
,因此您应该使用它为您提供的工具。
Spring uses jackson's ObjectMapper
for serialization and deserialization by default. Spring默认使用jackson的
ObjectMapper
进行序列化和反序列化。 A bean of this type is preconfigured for you, you can autowire it in your controller method and use it.这种类型的 bean 是为您预先配置的,您可以在 controller 方法中自动装配它并使用它。 Then the entire parsing is this:
那么整个解析是这样的:
int[][] parsedCoordinates = objectMapper.readValue(coordinates, int[][].class);
And your controller method looks like this:您的 controller 方法如下所示:
@PostMapping("/hole/coordinate")
@ResponseBody
public String saveCoordinate(@RequestBody Map<String, Object> params, ObjectMapper objectMapper) {
System.out.println("params = " + params);
System.out.println("params = " + params.get("coordinate"));
//get string from your params
String coordinates = "[[445, 292], [585, 331], [612, 223], [205, 532]]";
int[][] parsedCoordinates;
try {
parsedCoordinates = objectMapper.readValue(coordinates, int[][].class);
} catch (JsonProcessingException exc) {
//that's a bad way to handle error, but it's an example
//you might return error message like - invalid coordinate format
//or whatever you need
exc.printStackTrace();
throw new RuntimeException(exc);
}
//printing parsed result to check it
for (int i = 0; i < parsedCoordinates.length; i++) {
int[] inner = parsedCoordinates[i];
for (int j = 0; j < inner.length; j++) {
System.out.printf("pos %d-%d, value %d%n", i, j, parsedCoordinates[i][j]);
}
}
return "success";
}
Try this. It creates a matrix of coordinates.
String str = "[[445, 292], [585, 331], [612, 223], [205, 532]]";
List<String> numbers = List.of(str.split(",| |\\[|\\]"));
List<String> onlyNumbers = numbers.stream()
.filter(number -> !number.equals("") && !number.equals(" "))
.toList();
Integer[][] coordinates = new Integer[onlyNumbers.size()][2];
int counter = 0;
for(int i=0; i < onlyNumbers.size() - 1; i+=2){
coordinates[counter][0] = Integer.valueOf(onlyNumbers.get(i));
coordinates[counter][1] = Integer.valueOf(onlyNumbers.get(i+1));
counter++;
}
You can try the below code if you are trying to convert String to int[]如果您尝试将 String 转换为 int[],则可以尝试以下代码
import java.util.stream.*;
public class MyClass {
public static void main(String args[]) {
String str = "[[445, 292], [585, 331], [612, 223], [205, 532]]";
int[] arr= Stream.of(str.replaceAll("[\\[\\]\\ ]", "").split(",")).mapToInt(Integer::parseInt).toArray();
for (int i : arr) {
System.out.println("i = " + i);
}
}
}
It prints below values as output:它将以下值打印为 output:
i = 445
i = 292
i = 585
i = 331
i = 612
i = 223
i = 205
i = 532
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.