[英]Converting JavaScript Multi dimension Array into Java Array
I am using HandsOnTable to perform some business operation but while saving the form i retrieve data with method 我正在使用HandsOnTable执行一些业务操作,但是在保存表单时我使用方法检索数据
handsOnTable.getData();
which returns something like (JavaScript Array) 返回类似(JavaScript Array)的内容
[[1,2,3],[4,5,6],[7,8,9]]
but I am not able to figure out a way to convert this into 但我无法找出一种将其转换为
List<List<Integer>>
GSON will do this for you automatically. GSON将自动为您执行此操作。 I think this will get it done: 我认为这可以完成它:
Gson gson = new Gson();
String json = "[[1,2,3],[4,5,6],[7,8,9]]";
java.lang.reflect.Type.Type listOfListsOfIntsType = new com.google.gson.reflect.TypeToken.TypeToken<List<List<Integer>>>(){}.getType();
List<List<Integer>> list = gson.fromJson(json, listOfListsOfIntsType);
You can skip the TypeToken
business if you define a class with a member variable of type List<List<Integer>>
and just pass that class as the 2nd argument to fromJson()
. 如果您定义一个成员变量类型为List<List<Integer>>
类,并且仅将该类作为第二个参数传递给fromJson()
则可以跳过TypeToken
业务。
try this: 尝试这个:
int[][] dataArray = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
List<List<Integer>> dataList = new ArrayList<List<Integer>>();
for (int i = 0; i < dataArray.length; i++) {
List<Integer> tmpList = new ArrayList<Integer>();
dataList.add(tmpList);
for (int j = 0; j < dataArray[i].length; j++) {
tmpList.add(dataArray[i][j]);
System.out.println(dataArray[i][j]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.