来自Array的随机图像

编程入门 行业动态 更新时间:2024-10-21 18:56:39
来自Array的随机图像 - Android(Random Images from Array - Android)

嗨,我收到错误“意外令牌”为(int i = 0; i <4; i ++)

我有两个阵列,里面有相同的图像。 在这个例子中,我有2个阵列,有3个图像(每个阵列中的图像相同)。 我想从每个阵列获得2个随机图像,它们必须是相同的图像(因此是一对)。 关于我做错什么的任何输入都会很棒!

然后我想将这些图像放在图像按钮上。

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; }

}

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; }

}

最满意答案

如果它们保持相同的数据,为什么要使用两个数组?

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};

做就是了:

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

另外,我不明白你为什么要使用数组列表和while循环。 我会这样做:

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

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)]; }

更多推荐

int,图像,new,images,电脑培训,计算机培训,IT培训"/> <meta name="description

本文发布于:2023-08-02 04:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1368397.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   Array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!