Here's my problem : I have this String for instance
[[0,0,1],[1,0,0],[1,1,1]]
And I would like to convert it into an actual
int[][]
What's the best way to proceed ?
Thank you in advance !
Here's my problem : I have this String for instance
[[0,0,1],[1,0,0],[1,1,1]]
And I would like to convert it into an actual
int[][]
What's the best way to proceed ?
Thank you in advance !
The string you have is a lot like JSON format. You can use a JSON parser like gson
https://code.google.com/p/google-gson/
It will be able to parse string variants of lots of things.
The default fromJson might work since you are using pretty simple value types, but you can also try to explicitly set the array type.
Type listType = new TypeToken<Integer[][]>() {}.getType();
Integer[][] yourArray = new Gson().fromJson(text, listType);
Use nested loops and the Integer.parseInt
method.
You can ignore the first [ and the last ] as they are not making a difference. The you can split the string by the commas and get a String[]. You just need to iterate through that and fill up your int[][].