设为首页 加入收藏

TOP

System.arraycopy方法的使用
2014-11-23 22:12:34 】 浏览:434
Tags:System.arraycopy 方法 使用

  import java.util.Arrays;


  /**


  * 从指定源数组中复制一个数组,复制从指定的位置开始,


  *到目标数组的指定位置结束


  */


  public class LessionSystemArraycopy {


  public static void main(String[] args) {


  // 此方位为native方法。


  // public static native void arraycopy(


  // Object src, int srcPos, Object dest, // int destPos, int length);


  //初始化


  int[] ids = { 1, 2, 3, 4, 5 }; System.out.println(Arrays.toString(ids)); // [1, 2, 3, 4, 5]


  //测试自我复制


  //把从索引0开始的2个数字复制到索引为3的位置上


  System.arraycopy(ids, 0, ids, 3, 2); System.out.println(Arrays.toString(ids)); // [1, 2, 3, 1, 2]


  //测试复制到别的数组上


  //将数据的索引1开始的3个数据复制到目标的索引为0的位置上


  int[] ids2 = new int[6];


  System.arraycopy(ids, 1, ids2, 0, 3); System.out.println(Arrays.toString(ids2)); // [2, 3, 1, 0, 0, 0]


  //此功能要求


  //源的起始位置+长度不能超过末尾


  //目标起始位置+长度不能超过末尾


  //且所有的参数不能为负数


  try {


  System.arraycopy(ids, 0, ids2, 0, ids.length + 1);


  } catch (IndexOutOfBoundsException ex) {


  // 发生越界异常,数据不会改变System.out.println("拷贝发生异常:数据越界。");


  }


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇添加表时需要配置一下的目录和文件 下一篇HttpClient实现COOKIE方法

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目