从数组中随机抽取几个不重复的元素的几种实现方式(Java语言)

从数组中随机抽取几个不重复的元素的几种实现方式(Java语言)

需要从数组中随机抽取几个不重复的元素。

一、利用集合Set自动剔除选取的重复元素

来源:https://blog.51cto.com/u_16175449/7395618

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class RandomArrayElement {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        Random random = new Random();
        int count = random.nextInt(array.length);	//抽取的个数也是随机的
        Set<Integer> set = new HashSet<>();			//集合,自动排重
        while (set.size() < count) {
            int index = random.nextInt(array.length);	// 随机生成一个[0-array.length)的整数
            set.add(array[index]);
        }
        System.out.println("抽取到的元素:");
        for (int element : set) {
            System.out.println(element);
        }
    }
}

二、利用Random生成一个索引数组,遍历索引数组取得目标数组中的元素

来源:https://blog.51cto.com/u_16213341/7256808

import java.util.Random;

public class RandomArray {
    public static void main(String[] args) {
        String[] students = {"Alice", "Bob", "Charlie", "David", "Emma", "Frank", "Grace"};

        int numToSelect = 3; // 指定要抽取的学生人数

        String[] selectedStudents = selectRandomStudents(students, numToSelect);

        System.out.println("Selected students:");
        for (String student : selectedStudents) {
            System.out.println(student);
        }
    }

    public static String[] selectRandomStudents(String[] students, int numToSelect) {
        Random random = new Random();
        //核心代码
        int[] indexes = random.ints(0, students.length).distinct().limit(numToSelect).toArray();

        String[] selectedStudents = new String[numToSelect];
        for (int i = 0; i < numToSelect; i++) {
            selectedStudents[i] = students[indexes[i]];
        }
        return selectedStudents;
    }
}

三、利用Collections.shuffle()洗牌,然后抽取几个想要的元素

来源:https://blog.51cto.com/u_16175455/6770730

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class RandomArrayElement {
    public static void main(String[] args) {
        String[] names = {"Alice", "Bob", "Charlie", "David", "Emily", "Frank"};
        int numToSelect = 3; // 需要抽取的元素个数
        
        List<String> nameList = new ArrayList<>(Arrays.asList(names));
        Collections.shuffle(nameList); // 将名字列表随机排序【核心代码】
        
        List<String> selectedNames = new ArrayList<>();
        for (int i = 0; i < numToSelect; i++) {
            String selectedName = nameList.get(i); // 获取随机元素
            selectedNames.add(selectedName); // 将元素添加到已选列表
            nameList.remove(selectedName); // 从名字列表中移除已选元素
        }
        
        System.out.println("随机选取的名字是:" + selectedNames);
    }
}

四、每次随机选一个数,然后删除该元素后继续抽取

来源:https://blog.51cto.com/u_16175499/7648566

import java.util.Arrays;
import java.util.Random;

public class RandomSample {

    public static void main(String[] args) {
        String[] array = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};

        int n = 5; // 需要抽取的元素个数

        String[] result = new String[n];
        Random random = new Random();

        for (int i = 0; i < n; i++) {
            int index = random.nextInt(array.length); // 生成随机索引
            result[i] = array[index]; // 添加到新数组中
            array = removeElement(array, index); // 删除原数组中的元素
        }

        System.out.println(Arrays.toString(result));

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

唐骁虎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值