2.数组的复制

1.System.arraycopy
底层提供的方法,所以该方法native修饰

   /*
    *
    * @param      src      the source array. 复制对象
    * @param      srcPos   starting position in the source array. 复制数据起始位置
    * @param      dest     the destination array. 目标数组
    * @param      destPos  starting position in the destination data. 目标数组起始位置
    * @param      length   the number of array elements to be copied. 复制个数
    * @exception  IndexOutOfBoundsException  if copying would cause
    *               access of data outside array bounds.
    * @exception  ArrayStoreException  if an element in the <code>src</code>
    *               array could not be stored into the <code>dest</code> array
    *               because of a type mismatch.
    * @exception  NullPointerException if either <code>src</code> or
    *               <code>dest</code> is <code>null</code>.
    */
   public static native void arraycopy(Object src,  int  srcPos,
                                       Object dest, int destPos,
                                       int length);

2.简单实现数组复制

    public static void main(String[] args) {
        Integer[] a = new Integer[]{1, 2, 3, 4};
        Integer[] a1 = new Integer[3];
        //从a下标1开始复制3个
        System.arraycopy(a, 1, a1, 0, a1.length);
        System.out.println(Arrays.asList(a1));
    }

输出结果

[2, 3, 4]

3.删除指定数组下标的元素

    public static void main(String[] args) {
//        Integer[] a = new Integer[]{1, 2, 3, 4};
//        Integer[] a1 = new Integer[3];
//        //从a下标1开始复制3个
//        System.arraycopy(a, 1, a1, 0, a1.length);
//        System.out.println(Arrays.asList(a1));
        Integer[] a = new Integer[]{1, 2, 3, 4};
//        System.arraycopy(a, 1, 1, 1, a.length - 1);
//        a[a.length - 1] = null;
        deleteIndex(a, 0);
        System.out.println(Arrays.asList(a));
    }

    public static void deleteIndex(Integer[] arr, int index) {
        int number = arr.length - 1 - index;
        System.arraycopy(arr, index+1, arr, index , number);
        System.out.println(Arrays.asList(arr));
        arr[arr.length - 1] = null;
    }

输出结果:

[2, 3, 4, 4]
[2, 3, 4, null]
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 34,278评论 18 399
  • 一、基本数据类型 注释 单行注释:// 区域注释:/* */ 文档注释:/** */ 数值 对于byte类型而言...
    龙猫小爷阅读 4,417评论 0 16
  • 整整一个冬季 没有下雪了,风一脸的旧社会 ,忽冷忽热。 芦苇在河岸瑟瑟发抖,坚强的思考着生命的深刻。 正月初三,有...
    宁民阅读 183评论 0 0
  • 春天已经挥手告别,不要再追。让我们张开怀抱,迎接夏天的来临。 夏天雨水多,因为天空在流泪。立夏,一路有鲜花扑面,于...
    惠茹姐姐阅读 1,886评论 0 2
  • 天气冷得有点过分了。她盘坐在冰冷的床上想着。虽在亚热带的城市,空气里温度也没达到无法忍受的程度,然而一切周围的事物...
    西凉之夏阅读 271评论 0 0

友情链接更多精彩内容