从数组中随机抽取几个不重复的元素的几种实现方式(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));

你可能感兴趣的:(java,算法,开发语言)