reader reader - 25 days ago 6
Java Question

how to change the name of array in android script

i have arrays say for ex

int img1[] = {R.drawable.a1,R.drawable.a2,....}
int img2[] = {R.drawable.b1,R.drawable.b2,....}
int img3[] = {R.drawable.c1,R.drawable.c2,....}


I want to define a varibale for ex imgn
where when on some condition it has to select the different array
imgn= img1 or imgn= img2 etc

if (rnd.nextInt(4) == 1) (
imgn[rnd.nextInt(3)]);


where rnd is defined as Random

Regards

Answer

You can use HashMap

HashMap<String,int[]> arraynames = new HashMap<String,int[]>();
arraynames.put("img1", new int[]{R.drawable.a1,R.drawable.a2,....});
int[]arr=arraynames.get("img"+rnd.nextInt(3));

DEMO