-1

I have this kind of data. This can be or don't be an array. Just for easy reference.

ArrayName = Array1, Array2, Array3

Array1 = abc, cde, fgh

Array2 = abc, cde

Array3 = abc, cde, fgh, ijk, lmn

So, what are the best method to store this kind of data.

If I want to

  1. Add or delete Array1 and all things inside
  2. Add or delete item in Array2(eg. adding fgh or remove cde)

Methods I discovered:

  1. SharedPreference Android Shared preferences example
  2. Arrays
  3. SQLite Android SQLite Example
  4. Text file

Please share the pro and cons of why you choose the method. Please also share if there are better ways to store this kind of data.

Kindly edit this if you found a better link or sample for other to reference.

Brandon Minnick
  • 11,396
  • 12
  • 55
  • 108
Anthony
  • 51
  • 7
  • **ArrayName = Array1, Array2, Array3** this array is array of arrays ? You want to save them permanently ? I have question because your option 2. is Arrays – Misha Akopov Dec 19 '17 at 08:15
  • @MishaAkopov Yes, they are arrays of arrays. Yes, to save it permanently.You can combine 2 and 4 also. – Anthony Dec 19 '17 at 08:17
  • Possible duplicate of [Which Android Data Storage Technique to use?](https://stackoverflow.com/questions/9986734/which-android-data-storage-technique-to-use) – Adinia Dec 19 '17 at 08:54

2 Answers2

1

Here are pros and cons for each solutions:

1) SharedPreference

You save simple key-value pairs here. So it is very hard to save array and complex structures in SharedPreference. So the solution will not work with arrays and arrays of arrays. It will be extremely(but not impossible) difficult to achieve what you want.

2) Arrays

Absolutely not! It is memory storage, so when you close app, or on process death, you will lose all data

3) SqlLite

I would add to this other Databases for android, like Realm. Good solution. It is structured storage for collection of data. It will be very easy for storing/retrieving data when it is structured as rows. Furthermore you can delete rows easily. You don't have to read whole structure (other arrays) when you need particular row, or particular array (table in this case)

4) TextFile

I don't recommend to store in a text file, but it is possible to do so, you can serialize those arrays to text file, and deserialize. But every time you have to do this, and to read whole structure and parse it even if you want only e.g. Array2. It can be slow when your data becomes bigger.

Misha Akopov
  • 9,542
  • 26
  • 54
  • 73
0

It's incredibly hard to give advice with such vague requirements, you apparently have data structured as an array of arrays of strings, and you want to store it persistently on Android - and that's basically all we know.

In addition to the solutions mentioned, I would consider using GSON to store this as JSON to disk. While read/write may not have optimal performance, it's very easy to model documents with things like arrays of arrays, and we have no way of knowing your performance requirements vs ease of use.

class MyData {
  public List<List<String>> data;
}

If you then have a MyData object, you could simply serialize it to a string, which could be written to a file on disk:

String json = new Gson().toJson(myData);

This would produce something like

{
  "data": [
    ["abc", "cde", "fgh"],
    ["abc", "cde"],
    ["abc", "cde", "fgh", "ijk", "lmn"]
  ]
}

which could easily be written to disk using e.g. standard File and BufferedWriter. You can then read it back and deserialize using:

MyData myData = new Gson().fromJson(json, MyData.class);
JHH
  • 6,514
  • 5
  • 28
  • 64