how to randomly select from list of variables (bitmaps) with incremental names using Iterator and/or loop ? (java, blackberry, bb)
I ha开发者_运维百科ve a big list (say about thousand) of .png bitmaps with incremental names:
_image1 = Bitmap.getBitmapResource("a1.png");
_image2 = Bitmap.getBitmapResource("a2.png");
_image3 = Bitmap.getBitmapResource("a3.png");
...
_image999 = Bitmap.getBitmapResource("a999.png");
_image1000 = Bitmap.getBitmapResource("a1000.png");
I need a code to select one bitmap and attach it to BitmapField myBitmapField, when integer myCounter obtains random value from 1 to 1000. I could do it by checking value of myCounter thousand times by using if and else :
if (myCounter == 1)
myBitmapField.setBitmap(_image1);
else if (myCounter == 2)
myBitmapField.setBitmap(_image2);
else if (myCounter == 3)
myBitmapField.setBitmap(_image3);
...
else if (myCounter == 1000)
myBitmapField.setBitmap(_image1000);
But that would be very long code. Is there a way of doing it using Loop and/or Iterator ? Something like this:
int i = 0;
while (i < 1000)
{
i = i + 1;
if (myCounter == i)
myBitmapField.setBitmap(_image[i]);
}
Is there an easy and short way of doing it ? Thank you very much for help! (Java for blackberry)
What about dynamically generating the name, like
myBitmapField.setBitmap(Bitmap.getBitmapResource("a" + myCounter + ".png"));
If you're concerned about resource duplication, you can check a cache first.
精彩评论