java之System.arraycopy()
来源:互联网 发布:网络培训挂机 编辑:程序博客网 时间:2024/05/21 23:33
原文address:
http://blog.csdn.net/e421083458/article/details/8839242
import java.util.Arrays;public class LessionSystemArraycopy { public static void main(String[] args) { int[] ids={1,2,3,4,5}; System.out.println(Arrays.toString(ids)); //测试自我复制 把从索引0开始的两个数字复制到索引为3的位置上 System.arraycopy(ids, 0, ids, 3, 2); System.out.println(Arrays.toString(ids)); //测试复制到别的数组上 将数据索引1开始的三个数据复制到目标索引为0的位置上 int[] ids2=new int[6]; System.arraycopy(ids, 1, ids2, 0, 3); System.out.println(Arrays.toString(ids2)); /* * 此功能要求 * 源地址起始位置+长度 不能超过末尾 * 目标地址起始位置+长度 不能超过末尾 * 且所有参数不能为负数 */ try { System.arraycopy(ids, 0, ids2, 0, ids.length+1); } catch (Exception e) { //发生越界异常,数据不会发生改变 System.out.println("拷贝发生异常:数据越界"); } System.out.println(Arrays.toString(ids2)); //如果是类型转换的问题 Object[] o1={1,2,3,4.5,6.7}; Integer[] o2=new Integer[5]; System.out.println(Arrays.toString(o2)); try { System.arraycopy(o1, 0, o2, 0, o1.length); } catch (Exception e) { //发生存储转换,部分成功的数据会被复制过去 System.err.println("拷贝发生异常,数据转换错误,无法存储,"); } System.out.println(Arrays.toString(o2)); }}
结果如下:
[1, 2, 3, 4, 5][1, 2, 3, 1, 2][2, 3, 1, 0, 0, 0]拷贝发生异常:数据越界[2, 3, 1, 0, 0, 0][null, null, null, null, null][1, 2, 3, null, null]拷贝发生异常,数据转换错误,无法存储,
阅读全文
0 0
- java之System.arraycopy()
- Java之System.arraycopy方法的使用
- Java System.arraycopy()
- java 解析System.arraycopy
- java System.arraycopy
- java中system.arraycopy
- Java System.arraycopy()方法
- Java System.arraycopy()方法
- java System.arraycopy
- java System.arraycopy
- java System.arrayCopy
- Java方法之--System.arraycopy方法和Arrays.copyOf()
- Java方法之--System.arraycopy方法和Arrays.copyOf()
- java的System.arraycopy()方法
- java中的System.arraycopy方法
- java.lang.System.arraycopy()方法
- java中System.arraycopy()使用
- System.arraycopy
- scrapy抓取豆瓣电影TOP250
- 零基础python入门——学python是为了好玩
- POJ 2785
- 华中农业大学第五届程序设计大赛 GSequence Number [树上二分]【数据结构】
- 数组变换-Java-牛客模拟三
- java之System.arraycopy()
- 编程第三十二天
- 谈谈企业的数据工作!——企业的数据分析能力金字塔
- Kotlin的学习日志(二)Control Flow
- SQL语句的执行顺序
- MyBatis二级缓存
- 1198: DNA Sorting
- Android studio 恢复代码
- Lua知识点七