0

I am using HandsOnTable to perform some business operation but while saving the form i retrieve data with method

handsOnTable.getData();

which returns something like (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>>
1
  • If the data is a String you can use a JSON library to parse it. Commented Sep 30, 2015 at 17:13

2 Answers 2

2

GSON will do this for you automatically. 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().

2
  • 1
    Thanks, Exactly What I was Searching. Edited in order to eliminate confusion of which package to import for Type and TypeToken Commented Oct 1, 2015 at 6:26
  • Good edit, thanks. (I was just auto-importing Type in NetBeans yesterday and the dropdown showed me about 50 choices of classes named Type to choose from...) Commented Oct 2, 2015 at 15:49
0

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]);
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.