0

I am building a program that randomly generates multidimensional list arrays of the type :

ArrayList(ArrayList(ArrayList(String)));

My challenge is to find a way to save and restore these array lists once they have been (randomly) generated. I believe one solution is to convert them to a string and restore the array from that string although I can't seem to find a way to do so (I tried Json and deepToString). Please let me know if anyone has an idea. Thanks.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);

    Gson gson = new Gson();

    if (savedInstanceState == null) {

//GEREATE MAP

        GenMap();
        jsonmaps = gson.toJson(maps);
    } else {
        maps = gson.fromJson(jsonmaps, new TypeToken<ArrayList<ArrayList<ArrayList<String>>>>() {
        }.getType());
    }

//LAYOUT MAP

    MapLayout();
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    jsonmaps = savedInstanceState.getString("Sjsonmaps");
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("Sjsonmaps", jsonmaps);

}
1
  • Why "Serializing to JSON" didn't work ? Commented May 8, 2016 at 9:42

2 Answers 2

1

The following program shows converting multidimensional array to jsonString and restore back to multidimensional array using same jsonString. You can also store converted json String and restore back when required.

public static void main(String[] args) {
    ArrayList<ArrayList<ArrayList<String>>> maps = new ArrayList<ArrayList<ArrayList<String>>>();
    Gson gson = new Gson();
    ArrayList<String> list10 = new ArrayList<String>();
    list10.add("str10");
    list10.add("str11");
    list10.add("str12");

    ArrayList<String> list11 = new ArrayList<String>();
    list11.add("str20");
    list11.add("str22");
    list11.add("str23");

    ArrayList<ArrayList<String>> list20 = new ArrayList<ArrayList<String>>();
    list20.add(list10);
    list20.add(list11);

    ArrayList<ArrayList<String>> list21 = new ArrayList<ArrayList<String>>();

    ArrayList<String> list13 = new ArrayList<String>();
    list13.add("str30");
    list13.add("str31");
    list13.add("str32");

    list21.add(list13);

    maps.add(list20);
    maps.add(list21);

    String jsonString = gson.toJson(maps);
    System.out.println(jsonString);  // maps is converted to jsonString
    // converted value is [[["str10","str11","str12"],["str20","str22","str23"]],[["str30","str31","str32"]]]

    ArrayList<ArrayList<ArrayList<String>>> genratedMap = gson.fromJson(jsonString, new TypeToken<ArrayList<ArrayList<ArrayList<String>>>>() {}.getType());

    System.out.println("genratedMap : " + genratedMap); // jsonString restored back to maps (ArrayList)
}

Note: In above program i have used Gson Library - https://github.com/google/gson

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, gson seems like an elegant solution to what im looking to do. I tried to implement it with a save/restore approach but it stills seem to generate the map randomly (running the GenMap void) every time i open the activity. Any ideas as to what that might be (ive pasted the code above)?Cheers
0

You could use standard serialization, since ArrayList and String are serializable.

Here is an example:

public static void main(String args[]) throws Exception{

    ArrayList<String> obj = new ArrayList<>();

    obj.add("Hello");

    System.out.println(obj);

    /* Writing */
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myFile"));
    oos.writeObject(obj);

    oos.close();

    /* Reading */
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myFile"));
    ArrayList<String> obj2 = (ArrayList<String>) ois.readObject();

    ois.close();

    System.out.println(obj2);
}

2 Comments

Thanks for this, I tried to implement the I/O Streams but i'm still cant store the randomly generated maps (see code above), anything im doing wrong?
@You're reading the maps but not storing them. You've created a new local variable that hides your field. In the else block, remove the ArrayList<ArrayList<ArrayList<String>>> before maps.

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.