ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java Arrays.copyOfRange使用方法

2019-09-10 14:00:24  阅读:276  来源: 互联网

标签:index java Arrays copyOfRange range int length array original


源码

copyOfRange方法有以下几个重载的方法,使用方法基本一样,只是参数数组类型不一样

  • original:第一个参数为要拷贝的数组对象
  • from:第二个参数为拷贝的开始位置(包含)
  • to:第三个参数为拷贝的结束位置(不包含)

各个方法的源码基本一样,我们选取一个看下

可以看到内部实现实际是调用了System.arraycopy数组拷贝方法

Math.min(original.length - from, newLength)这行代码表示,若拷贝的内容超出源数组的数组边界,则只拷贝from位置到源数组最后一个元素,防止数组越界

/**
 * Copies the specified range of the specified array into a new array.
 * The initial index of the range (<tt>from</tt>) must lie between zero
 * and <tt>original.length</tt>, inclusive.  The value at
 * <tt>original[from]</tt> is placed into the initial element of the copy
 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
 * Values from subsequent elements in the original array are placed into
 * subsequent elements in the copy.  The final index of the range
 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
 * may be greater than <tt>original.length</tt>, in which case
 * <tt>0</tt> is placed in all elements of the copy whose index is
 * greater than or equal to <tt>original.length - from</tt>.  The length
 * of the returned array will be <tt>to - from</tt>.
 *
 * @param original the array from which a range is to be copied
 * @param from the initial index of the range to be copied, inclusive
 * @param to the final index of the range to be copied, exclusive.
 *     (This index may lie outside the array.)
 * @return a new array containing the specified range from the original array,
 *     truncated or padded with zeros to obtain the required length
 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
 *     or {@code from > original.length}
 * @throws IllegalArgumentException if <tt>from &gt; to</tt>
 * @throws NullPointerException if <tt>original</tt> is null
 * @since 1.6
 */
public static int[] copyOfRange(int[] original, int from, int to) {
    int newLength = to - from;
    if (newLength < 0)
        throw new IllegalArgumentException(from + " > " + to);
    int[] copy = new int[newLength];
    System.arraycopy(original, from, copy, 0,
            Math.min(original.length - from, newLength));
    return copy;
}

使用

public class Test {
    public static void main(String[] args) {
        int[] array = {0, 1, 2, 3, 4, 5, 6};
        int[] array2 = Arrays.copyOfRange(array, 2, 4);
        System.out.println(Arrays.toString(array2));
    }
}

结果输出如下,注意4位置是不包含的

[2, 3]

 

 

标签:index,java,Arrays,copyOfRange,range,int,length,array,original
来源: https://blog.csdn.net/mqdxiaoxiao/article/details/100693171

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有