從數組和List中隨機抽取若干不重復的元素


一、從數組中隨機抽取若干不重復元素

/**
 * @function:從數組中隨機抽取若干不重復元素
 *
 * @param paramArray:被抽取數組
 * @param count:抽取元素的個數
 * @return:由抽取元素組成的新數組
 */
public static String[] getRandomArray(String[] paramArray,int count){
        if(paramArray.length<count){
        return paramArray;
        }
        String[] newArray=new String[count];
        Random random= new Random();
        int temp=0;//接收產生的隨機數
        List<Integer> list=new ArrayList<Integer>();
        for(int i=1;i<=count;i++){
        temp=random.nextInt(paramArray.length);//將產生的隨機數作為被抽數組的索引
        if(!(list.contains(temp))){
        newArray[i-1]=paramArray[temp];
        list.add(temp);
        }
        else{
        i--;
        }
        }
        return newArray;
        }
二、從list中隨機抽取若干不重復元素

/**
 * @function:從list中隨機抽取若干不重復元素
 *
 * @param paramList:被抽取list
 * @param count:抽取元素的個數
 * @return:由抽取元素組成的新list
 */
public static List getRandomList(List paramList,int count){
        if(paramList.size()<count){
        return paramList;
        }
        Random random=new Random();
        List<Integer> tempList=new ArrayList<Integer>();
        List<Object> newList=new ArrayList<Object>();
        int temp=0;
        for(int i=0;i<count;i++){
        temp=random.nextInt(paramList.size());//將產生的隨機數作為被抽list的索引
        if(!tempList.contains(temp)){
        tempList.add(temp);
        newList.add(paramList.get(temp));
        }
        else{
        i--;
        }
        }
        return newList;
        }
或者如下方法:
思路1:利用List來把數組保存起來,在每取出一個元素后就刪除這個元素。
/**
 * 使用一個List來保存數組,每次隨機取出一個移除一個。
 */
public String[] getRandomArray(int n, String[] strArray){
        List<String> list = new ArrayList<String>();
        for(int i=0; i<strArray.length; i++){
        list.add(strArray[i]);
        }
        Random random = new Random();

        // 當取出的元素個數大於數組的長度時,返回null
        if(n>list.size()){
        return null;
        }

        String[] result = new String[n];
        for(int i=0; i<n; i++){
        // 去一個隨機數,隨機范圍是list的長度
        int index = random.nextInt(list.size());
        result[i] = list.get(index);
        list.remove(index);
        }

        return result;
        }
思路2:在使用一個boolean型數組保存對應數組中元素是否被取出的狀態。
/**
 * 使用一個兩個數組,一個來保存元素,一個用來保存對應元素是否被取走
 */
public String[] getRandomArray(int n, String[] strArray){
        // 當取出的元素個數大於數組的長度時,返回null
        if(n>strArray.length){
        return null;
        }

        // 定義一個域strArray相同長度的數組,每個位置保存對應位置是否被取走
        boolean[] bool = new boolean[strArray.length];
        for(int i=0; i<strArray.length; i++){
        bool[i] = false;
        }

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

        for(int i=0; i<n; i++){
        int index;
        // 判斷隨機的位置是否被取走,取走則繼續循環
        do{
        index = random.nextInt(n);
        }while(bool[index]);

        // 取出元素,將其對應位置設置為true
        bool[index] = true;
        result[i] = strArray[index];
        }

        return result;
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM