简体   繁体   中英

Random Images from Array - Android

Hi I am getting the error "unexpected token" at for (int i=0; i<4; i++)

I have two arrays with the same images inside. In this example I have 2 arrays with 3 images (images are the same in each array). I want to get 2 random images from each array and they must be the same image (therefore being a pair). Any input on what im doing wrong would be great!

THEN I want to put these images onto image buttons.

public class Card {
ArrayList<Integer> list=new ArrayList<Integer>();
ArrayList<Integer> list1=new ArrayList<Integer>();
Random r1=new Random();
int[] imageArray1 = new int[] {R.drawable.raptors, R.drawable.okc_thunder, R.drawable.spurs};

int[] imageArray2 = new int[] {R.drawable.raptors, R.drawable.okc_thunder, R.drawable.spurs};
for(int i=0;i<4;i++)
{
    while(true)
    {
        int next=r1.nextInt(10)+1;
        if(!list.contains(next))
        {
            list.add(imageArray1[next]);
            list1.add(imageArray2[next]);
            break;
        }
    }
}
array1 = convertIntegers(list);
array2 = convertIntegers(list1);


public static int[] convertIntegers(List<Integer> integers)
{
    int[] ret = new int[integers.size()];
    Iterator<Integer> iterator = integers.iterator();
    for (int i = 0; i < ret.length; i++)
    {
        ret[i] = iterator.next().intValue();
    }
    return ret;
}

}

Why use two arrays if they hold the same data anyway?

int[] imageArray1 = new int[] {R.drawable.raptors, R.drawable.okc_thunder, R.drawable.spurs};
int[] imageArray2 = new int[] {R.drawable.raptors, R.drawable.okc_thunder, R.drawable.spurs};

Just do:

int[] imageArray = new int[] {R.drawable.raptors, R.drawable.okc_thunder, R.drawable.spurs};

Also I don't get the point why you're using array lists and while loops. I would do it like this:

public int getRandomCard() {
    return this.imageArray[r1.nextInt(3)];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM