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 閱讀(464) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          公告

          希望有一天

          我能用鼠標雙擊我的錢包

          然后選中一張100元

          按住“ctrl+c”

          接著不停的“ctrl+v”

          嘻嘻~~~笑醒~~~



          導航

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

          統計

          常用鏈接

          留言簿(6)

          隨筆分類(102)

          隨筆檔案(398)

          文章分類

          文章檔案(10)

          有趣網絡

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 黄平县| 湾仔区| 溧水县| 满城县| 克拉玛依市| 天柱县| 屏东市| 呼玛县| 临沂市| 甘洛县| 福建省| 桦川县| 金寨县| 辉南县| 兰坪| 皋兰县| 西充县| 博兴县| 明光市| 喀喇| 稻城县| 罗定市| 绍兴县| 西城区| 平武县| 胶南市| 刚察县| 湘乡市| 安新县| 开江县| 澳门| 沁源县| 丹阳市| 环江| 彰武县| 治县。| 涪陵区| 宜章县| 遵义县| 合江县| 乐至县|