免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
隨機(jī)子集的Java實(shí)現(xiàn)
獲取一個(gè)集合中指定個(gè)數(shù)的隨機(jī)子集(元素不重復(fù))的實(shí)現(xiàn)。
package com.lavasoft.randomset;
import java.util.*;
/**
* 數(shù)學(xué)集合概念上的隨機(jī)子集的Java實(shí)現(xiàn)研究代碼
*
* @author leizhimin 2010-5-17 13:25:52
*/
public class RundomSubsetToolkit {
public static void main(String[] args) {
//                int[] rs = randomSubset(1, 5000, 300);
//                for (int r : rs) {
//                        System.out.println(r);
//                }
//                List list = new ArrayList(10);
//                list.add("a");
//                list.add("b");
//                list.add("c");
//                list.add("d");
//                list.add("e");
//                list.add("f");
//                list.add("g");
//                list.add("h");
//                list.add("i");
//                list.add("j");
//                List rs1 = randomSubset(list, 5);
//                for (Object o : rs1) {
//                        System.out.println(o);
//                }
//
int[] array = {7, 9, 2, 8, 6, 4, 3};
int[] rs3 = randomSubset(array, 4);
for (int i : rs3) {
System.out.println(i);
}
Map map = new HashMap();
}
/**
* 獲取某個(gè)范圍內(nèi)指定個(gè)數(shù)的隨機(jī)自然數(shù)子集
*
* @param beginIndex 取之范圍起
* @param endIndex     取之范圍止
* @param subCount     子集元素?cái)?shù)目
* @return 自然數(shù)子集
*/
public static int[] randomSubset(final int beginIndex, final int endIndex, final int subCount) {
if (beginIndex < 0 || endIndex < 1 || subCount < 0 || endIndex - beginIndex < subCount) {
throw new RuntimeException("獲取隨機(jī)子集的參數(shù)不合邏輯!");
}
int[] rs = new int[subCount];
int rescount = endIndex - beginIndex + 1;
int[] scope = new int[rescount];
for (int i = beginIndex; i <= endIndex; i++)
scope[i - beginIndex] = i;
Random random = new Random();
for (int i = 0, odd = rescount - 1; i < subCount; i++, odd--) {
int ranindex = random.nextInt(odd);
rs[i] = scope[ranindex];
scope[ranindex] = scope[odd];
}
return rs;
}
/**
* 獲取一個(gè)集合列表指定數(shù)目的子集
*
* @param list         集合列表
* @param subCount 子集數(shù)目
* @return 子集
*/
public static List randomSubset(final List list, final int subCount) {
if (list == null || list.size() == 0 || list.size() < subCount) {
throw new RuntimeException("獲取隨機(jī)子集的參數(shù)不合邏輯!");
}
List rs = new ArrayList(subCount);
for (int i = 0; i < subCount; i++) {
rs.add(null);
}
Random random = new Random();
for (int i = 0, odd = list.size() - 1; i < subCount; i++, odd--) {
int ranindex = random.nextInt(odd);
rs.set(i, list.get(ranindex));
list.set(ranindex, list.get(odd));
}
return rs;
}
/**
* 獲取一個(gè)long型數(shù)組中指定數(shù)目的子集
*
* @param array        數(shù)組
* @param subCount 子集數(shù)目
* @return 子集
*/
public static long[] randomSubset(long[] array, int subCount) {
long[] rs = new long[subCount];
if (array == null || array.length == 0 || array.length < subCount) {
throw new RuntimeException("獲取隨機(jī)子集的參數(shù)不合邏輯!");
}
Random random = new Random();
for (int i = 0, odd = array.length - 1; i < subCount; i++, odd--) {
int ranindex = random.nextInt(odd);
rs[i] = array[ranindex];
array[ranindex] = array[odd];
}
return rs;
}
/**
* 獲取一個(gè)int型數(shù)組中指定數(shù)目的子集
*
* @param array        數(shù)組
* @param subCount 子集數(shù)目
* @return 子集
*/
public static int[] randomSubset(int[] array, int subCount) {
int[] rs = new int[subCount];
if (array == null || array.length == 0 || array.length < subCount) {
throw new RuntimeException("獲取隨機(jī)子集的參數(shù)不合邏輯!");
}
Random random = new Random();
for (int i = 0, odd = array.length - 1; i < subCount; i++, odd--) {
int ranindex = random.nextInt(odd);
rs[i] = array[ranindex];
array[ranindex] = array[odd];
}
return rs;
}
/**
* 獲取一個(gè)Object型數(shù)組中指定數(shù)目的子集
*
* @param array        數(shù)組
* @param subCount 子集數(shù)目
* @return 子集
*/
public static Object[] randomSubset(Object[] array, int subCount) {
Object[] rs = new Object[subCount];
if (array == null || array.length == 0 || array.length < subCount) {
throw new RuntimeException("獲取隨機(jī)子集的參數(shù)不合邏輯!");
}
Random random = new Random();
for (int i = 0, odd = array.length - 1; i < subCount; i++, odd--) {
int ranindex = random.nextInt(odd);
rs[i] = array[ranindex];
array[ranindex] = array[odd];
}
return rs;
}
}
本文出自 “熔 巖” 博客,請(qǐng)務(wù)必保留此出處http://lavasoft.blog.51cto.com/62575/317297
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
奇偶排序問題
flash動(dòng)畫制作實(shí)例:打造粒子煙花效果
CART算法的簡單實(shí)現(xiàn)(1)
php 函數(shù) array_filter
[珠璣之櫝]二分思想與分治法、排序思想
我的Python筆記·NumPy入門(一)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服