arrays - How to create a "for" loop to add not only a value but also an indentifier. Android, Eclipse IDE -
let's have example array of integers, don't know length of array (it gets added shared preferences , grow in size each time app run):
int[] testarray = new int[unknown length];
and want create int names values array. ie.
int test1 = testarray[1]; int test2 = testarray[2]; int test3 = testarray[3]; .... int test999 = testarray[999];
let's assume length , final value, although in real app don't know.
so, started, have:
for (int = 0; < items.length; i++) { int test1 = testarray[i] }
however, add values i=0 , upwards testarray[i]. ie.
test1 = testarray[1]; test1 = testarray[2]; test1 = testarray[3];
can make "test1" change test2, test3 etc. using loop , i?
ie.
int testi = testarray[i].
of course line of code not work obvious reasons, there way such thing array , loop?? or need resort array list, or perhaps else?
data structure store set of values of unknown length)
// create vector , populate elements vector vector = new vector(); vector.add(new integer(1)); vector.add(new integer(2)); vector.add(new integer(3)); // elements() method returns enumeration of vector's elements
adding shared preferences
enumeration vectorelementsenum = vector.elements(); int counter=0; string arrayname = "myarr"; while(vectorelementsenum.hasmoreelements()) { editor.putint(arrayname + "_" + i, vectorelementsenum.nextelement()); editor.commit(); count++; } editor.putint(arrayname +"_size", counter); editor.commit();
retrieving shared preferences
sharedpreferences prefs = mcontext.getsharedpreferences("preferencename", 0); int size = prefs.getint(arrayname + "_size", 0); integer array[] = new integer [size]; for(int i=0;i<size;i++) array[i] = prefs.getint(arrayname + "_" + i, -1);
finally having var1=arr[1],var2=arr[2], etc uncalled because var1 same arr[1], why have data replicated? hope above implementation suits need.
Comments
Post a Comment