zoukankan      html  css  js  c++  java
  • 复制除给定下标之外的数组元素1

    /**

      * 复制除给定下标之外的数组元素

      * @param array 一个String数组

      * @param index 在长度之内的下标

      * @return 一个新数组,若原数组长度不大于1,返回null

      * @throws ArrayIndexOutOfBoundsException 下标值应该在数组范围内

      */

     public String[] copyExcept(String[] array, int index) throws ArrayIndexOutOfBoundsException {

    // 初始新数组为null

      String[] newArray = null;

    // 复制的原数组长度必须大于1,否则复制的数组将为null

    if (array != null && array.length > 1) {

       // index在数组下标之内,否则抛出异常

       if (index >= 0 && index < array.length) {

        // 新数组长度比原数组少一

        newArray = new String[array.length - 1];

        int newIndex = -1;// 初始新数组下标

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

         if (i == index) {

          continue;// 当等于给定下标值时,不进行复制

         } else {

     newIndex += 1;

          newArray[newIndex] = array[i];

         }

        }

       } else {

        throw new ArrayIndexOutOfBoundsException("下标值应该大于等于零且小于" + array.length);

       }

      }

      return newArray;

     }

  • 相关阅读:
    查找第K小数
    比较奇偶数个数
    哈夫曼树练习
    数字转二进制数练习
    随笔
    字符串反码(练习)
    eclipse构建maven的web项目
    mysql中的一些操作语句,留存
    urllib2功能说明
    Python-第三方库requests详解
  • 原文地址:https://www.cnblogs.com/quanby/p/5392504.html
Copyright © 2011-2022 走看看