Java 如何放置随机图像而不重复它们?

Java 如何放置随机图像而不重复它们?,java,android,android-studio,Java,Android,Android Studio,我曾尝试编写一个应用程序,其中出现了一些卡片,有必要将它们与其他卡片合并,问题是当我随机化它们时,一些卡片会重复出现,而另一些则不会出现 如何使图像看起来随机但不重复 我是这样做的 int[] images = {R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four}; Random rand = new Random(); imgSelected.setImageResource(images[ra

我曾尝试编写一个应用程序,其中出现了一些卡片,有必要将它们与其他卡片合并,问题是当我随机化它们时,一些卡片会重复出现,而另一些则不会出现

如何使图像看起来随机但不重复

我是这样做的

  int[] images = {R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four};
     Random rand = new Random();
     imgSelected.setImageResource(images[rand.nextInt(images.length)]);
      imgSelected2.setImageResource(images[rand.nextInt(images.length)]);
       imgSelected3.setImageResource(images[rand.nextInt(images.length)]);
        imgSelected4.setImageResource(images[rand.nextInt(images.length)]);

 int[] images2 = {R.id.imgfive, R.id.imgsix, R.id.imgseven, R.id.imgeight};

    //
                 Random rand2 = new Random();
    img1.setImageResource(images2[rand2.nextInt(images2.length)]);
    img2.setImageResource(images2[rand2.nextInt(images2.length)]);
    img3.setImageResource(images2[rand2.nextInt(images2.length)]);
    img4.setImageResource(images2[rand2.nextInt(images2.length)]);

在使用图像时将其删除

List<Integer> images....
Random rand = new Random;
int randomInt = rand.nextInt(images.size());
imgSelected.setImageResource(images[rand.nextInt(images.length)]);
images.remove(randomInt);
randomInt = rand.nextInt(images.size());
imgSelected2.setImageResource(images[rand.nextInt(images.length)]);
images.remove(randomInt);
randomInt = rand.nextInt(images.size());
imgSelected3.setImageResource(images[rand.nextInt(images.length)]);
images.remove(randomInt);
randomInt = rand.nextInt(images.size());
imgSelected4.setImageResource(images[rand.nextInt(images.length)]);
images.remove(randomInt);
列出图像。。。。
随机兰德=新的随机;
int randomInt=rand.nextInt(images.size());
imgSelected.setImageResource(images[rand.nextInt(images.length)]);
图像。删除(随机点);
randomInt=rand.nextInt(images.size());
imgSelected2.setImageResource(images[rand.nextInt(images.length)]);
图像。删除(随机点);
randomInt=rand.nextInt(images.size());
imgSelected3.setImageResource(images[rand.nextInt(images.length)]);
图像。删除(随机点);
randomInt=rand.nextInt(images.size());
imgSelected4.setImageResource(images[rand.nextInt(images.length)]);
图像。删除(随机点);

这样图像就不会在那里再次使用。

您可以使用
Collections.shuffle()
获得数组的随机排列