2

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

1 Answer 1

1

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

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

2 Comments

do we need to import any thing ? because it is not finding put fucntion in the arraynames.put("img1", new int[]{R.drawable.a1,R.drawable.a2,....}); iam using android.
we need to add values in constructor as suggested in [link] (stackoverflow.com/questions/34158839/…) thanks again

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.