I am stuck on something which first seemed very easy to me, but I just cannot figure it out. I want to store an array of integers (int[]) in SharedPreferences, preferably as a set and not as separate entries in SharedPreferences, because this way I am 100% sure that the order of this int[] will remain the same:
int[] myIntArray = new int[]{1,2,3};
Set valueSet = new HashSet(asList(myIntArray));
SharedPreferences sharedPreferences = context.getSharedPreferences("name",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putStringSet("key", valueSet);
editor.commit();
When I return this set
SharedPreferences sharedPreferences = context.getSharedPreferences("name",Context.MODE_PRIVATE);
Set set = sharedPreferences.getStringSet("key", null);
In debugging I end up with a set which is structured as follows:
->set {HashSet}
----->0 {int[]}
---------->0 = 1
---------->1 = 2
---------->2 = 3
I just cannot figure out how I can retrieve this int[] from within this set back into a local int[] variable.
The closest I get to my goal is by doing:
Set set = sharedPreferences.getStringSet("imageverdict", null);
Object[] o = set.toArray();
Object o1 = o[0];
This gives me in debugging
->o1 {int[]}
---->0 = 1
---->1 = 2
---->2 = 3
But when I try to retrieve any values from this by doing int i = o1[1];
I get an error in the editor: Array type expected; found 'java.lang.object'
.
This seems weird to me since during debugging o1 is shown as an int[], whilst within the editor android studio tells me it is an object.
Any help is appreciated!