我有一个处理数组的问题。
@Override
public void run() {
TypedArray images = getResources().obtainTypedArray(R.array.images_primes);
List<Integer> indexes = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
Collections.shuffle(indexes);
Log.d("MYAPP", "value: " + indexes);
int randomPrimeNumber = (int) (indexes());
// setImageResource to the random chosenImageNumber
imageViewMeasurement.setImageResource(images.getResourceId(randomPrimeNumber, R.color.colorGreyMeasuerementScreen));
}
它做了什么?
从资源文件中获取图像数组(工作) - &gt; (typedArray图片)。
列出索引=列表,只要&#34;图像&#34; - &GT;洗牌清单,有效! - &GT; D / MYAPP:价值:[4,9,10,5,3,1,7,2,5,6]
3/4。将是:浏览&#34;索引&#34;的洗牌列表,从&#34; images&#34;,setImageresource的数组中调用数字。
问题: 关于第2点:我可以创建列表索引依赖于&#34;图像&#34; -array的image.length吗?
关于第3-4点:我无法将两个数组(图像/混洗索引)放在一起。当我尝试设置
时,它显示有关无法将整数转换为int的错误int randomPrimeNumber = (int) (indexes);
例如,。
是否有可能&#34;混合&#34;两个阵列?
最好的,谢谢, tigercode
答案 0 :(得分:0)
没有理由创建indexes
列表,如果要生成小于images
数组长度的随机数,只需使用Random
:
Random random = new Random();
int randomNumber = random.nextInt(images.length());
是的,length()
类中有一个TypedArray
方法。