我自己突发奇想想到的一种快速产生一个随机数组,并且数组里面的数字唯一的方法,如有雷同,纯属虚构,呵呵
当然,这还可以引申,数组里面放其他的也是一样的,比如说id神马的。
方法:自己先弄一个数组,并且里面自己赋一组唯一的值,如int[] a=new int[]{1,2,3,4,5,6,7,8}; 。(要是0~100,循环赋值就ok)然后再生成两个随机数a,b,交换int[a]和int[b]的值,如此循环个几十次就行了
android代码:0~8随机排列
Random random1=new Random();
int[] currentPictureIds=new int[]{2,5,3,0,8,4,6,7,1};
int x,y,temp;
for (int i = 0; i < 10; i++) {
x=random1.nextInt(9);
y=random1.nextInt(9);
temp=currentPictureIds[x];
currentPictureIds[x]=currentPictureIds[y];
currentPictureIds[y]=temp;
}