我正在使用HandsOnTable执行某些业务操作但在保存表单时我使用方法检索数据
handsOnTable.getData();
返回类似(JavaScript Array)
的内容[[1,2,3],[4,5,6],[7,8,9]]
但我无法找到将其转换为
的方法List<List<Integer>>
答案 0 :(得分:2)
GSON会自动为您完成此操作。我认为这将完成它:
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);
如果您定义一个类型为TypeToken
的成员变量的类,并将该类作为第二个参数传递给List<List<Integer>>
,则可以跳过fromJson()
业务。
答案 1 :(得分:0)
试试这个:
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]);
}
}