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


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


          網站導航:
           

          公告

          希望有一天

          我能用鼠標雙擊我的錢包

          然后選中一張100元

          按住“ctrl+c”

          接著不停的“ctrl+v”

          嘻嘻~~~笑醒~~~



          導航

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

          統計

          常用鏈接

          留言簿(6)

          隨筆分類(102)

          隨筆檔案(398)

          文章分類

          文章檔案(10)

          有趣網絡

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 永顺县| 嘉禾县| 曲松县| 延边| 九龙县| 旅游| 阜城县| 林州市| 清流县| 丰镇市| 汪清县| 丽江市| 五莲县| 右玉县| 西贡区| 平乐县| 五原县| 渝中区| 泗水县| 灵台县| 响水县| 博白县| 永靖县| 扎鲁特旗| 甘洛县| 聂拉木县| 青浦区| 宜宾市| 灵川县| 湄潭县| 古交市| 新沂市| 乌拉特前旗| 永川市| 孟州市| 集安市| 斗六市| 彝良县| 濮阳县| 繁峙县| 桂阳县|