Java之System.arraycopy方法的使用
2013-04-23 13:00
197 查看
不废话,直接看代码就全清楚了。里面 // 后面的是运行的结果
import java.util.Arrays; /** * 老紫竹J***A提高教程 - System.arraycopy方法的使用。<br> * <br> * 从指定源数组中复制一个数组,复制从指定的位置开始,<br> * 到目标数组的指定位置结束 * * @author 老紫竹的家(java2000.net,laozizhu.com) * */ 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("拷贝发生异常:数据越界。"); } System.out.println(Arrays.toString(ids2)); // [2, 3, 1, 0, 0, 0] // 如果是类型转换问题 Object[] o1 = { 1, 2, 3, 4.5, 6.7 }; Integer[] o2 = new Integer[5]; System.out.println(Arrays.toString(o2)); // [null, null, null, null, null] try { System.arraycopy(o1, 0, o2, 0, o1.length); } catch (ArrayStoreException ex) { // 发生存储转换,部分成功的数据会被复制过去 System.out.println("拷贝发生异常:数据转换错误,无法存储。"); } // 从结果看,前面3个可以复制的数据已经被存储了。剩下的则没有 System.out.println(Arrays.toString(o2)); // [1, 2, 3, null, null] } }
相关文章推荐
- java 何时使用System.exit() 方法
- java.lang.System.arraycopy()方法使用说明
- java system.out.printf()的使用方法
- java system.out.printf()的使用方法
- Java中System.arraycopy方法的使用
- Java:System.loadLibrary()的使用方法汇总
- (java 必备知识之一) java 数组复制 System方法 arrayCopy 深入解析
- java system.out.printf()的使用方法 http://blog.csdn.net/wangchangshuai0010/article/details/8647503
- Java学习之System.arraycopy()方法
- JAVA System.arraycopy方法的使用
- java system.out.printf()的使用方法
- java.lang.System下的arraycopy和java.util.Arrays.copyOf方法
- java system.out.printf()的使用方法
- java system类使用方法示例 获取系统信息
- java system.out.printf()的使用方法
- 老紫竹JAVA提高教程-System.arraycopy方法的使用
- java system.out.printf()的使用方法
- Java中System.getProperty的使用方法
- JAVA中System.getProperty()方法使用
- 老紫竹JAVA提高教程-System.arraycopy方法的使用