Loading...

java .net

从指定的数组中随机取n个

import java.util.*;

public class Test
{
public static void main(String[] agrs)
{
   int[] allIdList = new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
   int[] randomIdList = new Test().getRandomIdList(allIdList,10);
   for(int randomIdList_index = 0;randomIdList_index < randomIdList.length;randomIdList_index++){
    System.out.println(randomIdList[randomIdList_index]);
   }
}


/**
*
* @author liuzhaochun
* @explain:从指定的数组中随机取count个,返回这个数组
* @datetime:2008-5-9
* @return
* @return int [] 包含随机取的count个值的数组
*/
public int[] getRandomIdList(int[] allIdList,int count){
  
   int[] randomIdList = new int[count];
   int randomIdList_index = 0;
   for(int allIdList_index = allIdList.length - 1; randomIdList_index < count;allIdList_index--,randomIdList_index++){
    int temp_Index = (int)(Math.random() * allIdList_index);
    randomIdList[randomIdList_index] = allIdList[temp_Index];
    allIdList[temp_Index] = allIdList[allIdList_index];
   }
   return randomIdList;
}



}

posted on 2008-08-26 22:06 阅读(453) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

公告

希望有一天

我能用鼠标双击我的钱包

然后选中一张100元

按住“ctrl+c”

接着不停的“ctrl+v”

嘻嘻~~~笑醒~~~



导航

<2008年8月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

常用链接

留言簿(6)

随笔分类(102)

随笔档案(398)

文章分类

文章档案(10)

有趣网络

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜