zoukankan      html  css  js  c++  java
  • 从一个数组中随机取出一定数量元素组成新数组

    /**

      * 从一个数组中随机取出一定数量元素组成新数组

      * @param array 一个String类型的数组

      * @param number需要取出元素的数量

      * @return 一个随机的数组

      * @throws NullPointerException原数组不能为空

      *@throws ArrayIndexOutOfBoundsException新数组长度应不大于原数组的长度

      */

     public static String[]  getRandomArray(String[] array, int number)

       throws NullPointerException, ArrayIndexOutOfBoundsException {

      Random random = new Random();

      String[] arrayNew = new String[number];// 新得到的数组

      String[] arrayTemp = array;// 变化中的数组,一开始等于原数组

      int index;// 定义变化中的数组数组下标

      for (int i = 0; i < arrayNew.length; i++) {

       index = random.nextInt(arrayTemp.length);随机得到一个数组下标

       arrayNew[i] = arrayTemp[index];// 复制得到新数组

       arrayTemp = copyExcept(arrayTemp, index);}

    return arrayNew;}

     }

      return arrayNew;

     }

  • 相关阅读:
    form表单
    JsonResponse对象
    HttpRequest对象
    Django基础
    ssh私钥登录
    监控window目录是否为空自定义item
    redis频繁报错Cannot allocate memory
    脱敏html文件文件
    MySQL删除数据
    ftp服务器搭建
  • 原文地址:https://www.cnblogs.com/quanby/p/5396829.html
Copyright © 2011-2022 走看看