- 浏览: 646436 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (213)
- JAVA (53)
- Tomcat (14)
- EXTJS专栏 (2)
- 数据库 (23)
- JavaScript (13)
- jQuery (11)
- 需求分析 (2)
- css (1)
- 手机 (1)
- php (7)
- 生活 (1)
- ssh (11)
- JFReeChart (2)
- Delphi (1)
- junit (0)
- liunx (10)
- 项目管理 (11)
- 文件下载 (1)
- Eclipse (2)
- jsp (2)
- 外挂、游戏 (1)
- 面试题 (8)
- 性能测试 (2)
- apache (1)
- MyBatis (1)
- Webservice (2)
- ant (1)
- IDEA (2)
- log4j (1)
- 小程序 (1)
最新评论
-
hll127:
请问service这个方法里为什么要while(true)啊? ...
jsp页面静态化例子 -
tarena1:
Spring 源码解读 推荐流程 -
konglx:
springaop_springmvc 写道可以参考最新的文档 ...
Web项目添加Maven支持步骤 -
springaop_springmvc:
可以参考最新的文档:如何在eclipse jee中检出项目并转 ...
Web项目添加Maven支持步骤 -
9976:
很好,谢谢
Spring 源码解读 推荐流程
System提供了一个静态方法arraycopy(),我们可以使用它来实现数组之间的复制。其函数原型是:
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
src:源数组; srcPos:源数组要复制的起始位置;
dest:目的数组; destPos:目的数组放置的起始位置; length:复制的长度。
注意:src and dest都必须是同类型或者可以进行转换类型的数组.
有趣的是这个函数可以实现自己到自己复制,比如:
int[] fun ={0,1,2,3,4,5,6};
System.arraycopy(fun,0,fun,3,3);
则结果为:{0,1,2,0,1,2,6};
实现过程是这样的,先生成一个长度为length的临时数组,将fun数组中srcPos
到srcPos+length-1之间的数据拷贝到临时数组中,再执行System.arraycopy(临时数组,0,fun,3,3).
评论
3 楼
hikaru1012
2010-10-09
楼主解释的正确。只有在源数组和目的数组是同一个数组时才会创建临时数组。
2 楼
hikaru1012
2010-10-09
java.lang.System.arraycopy注释
void java.lang.System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest. The number of components copied is equal to the length argument. The components at positions srcPos through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, respectively, of the destination array.
If the src and dest arguments refer to the same array object, then the copying is performed as if the components at positions srcPos through srcPos+length-1 were first copied to a temporary array with length components and then the contents of the temporary array were copied into positions destPos through destPos+length-1 of the destination array.
If dest is null, then a NullPointerException is thrown.
If src is null, then a NullPointerException is thrown and the destination array is not modified.
Otherwise, if any of the following is true, an ArrayStoreException is thrown and the destination is not modified:
The src argument refers to an object that is not an array.
The dest argument refers to an object that is not an array.
The src argument and dest argument refer to arrays whose component types are different primitive types.
The src argument refers to an array with a primitive component type and the dest argument refers to an array with a reference component type.
The src argument refers to an array with a reference component type and the dest argument refers to an array with a primitive component type.
Otherwise, if any of the following is true, an IndexOutOfBoundsException is thrown and the destination is not modified:
The srcPos argument is negative.
The destPos argument is negative.
The length argument is negative.
srcPos+length is greater than src.length, the length of the source array.
destPos+length is greater than dest.length, the length of the destination array.
Otherwise, if any actual component of the source array from position srcPos through srcPos+length-1 cannot be converted to the component type of the destination array by assignment conversion, an ArrayStoreException is thrown. In this case, let k be the smallest nonnegative integer less than length such that src[srcPos+k] cannot be converted to the component type of the destination array; when the exception is thrown, source array components from positions srcPos through srcPos+k-1 will already have been copied to destination array positions destPos through destPos+k-1 and no other positions of the destination array will have been modified. (Because of the restrictions already itemized, this paragraph effectively applies only to the situation where both arrays have component types that are reference types.)
パラメーター:
src the source array.
srcPos starting position in the source array.
dest the destination array.
destPos starting position in the destination data.
length the number of array elements to be copied.
スロー:
IndexOutOfBoundsException - if copying would cause access of data outside array bounds.
ArrayStoreException - if an element in the src array could not be stored into the dest array because of a type mismatch.
NullPointerException - if either src or dest is null.
void java.lang.System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest. The number of components copied is equal to the length argument. The components at positions srcPos through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, respectively, of the destination array.
If the src and dest arguments refer to the same array object, then the copying is performed as if the components at positions srcPos through srcPos+length-1 were first copied to a temporary array with length components and then the contents of the temporary array were copied into positions destPos through destPos+length-1 of the destination array.
If dest is null, then a NullPointerException is thrown.
If src is null, then a NullPointerException is thrown and the destination array is not modified.
Otherwise, if any of the following is true, an ArrayStoreException is thrown and the destination is not modified:
The src argument refers to an object that is not an array.
The dest argument refers to an object that is not an array.
The src argument and dest argument refer to arrays whose component types are different primitive types.
The src argument refers to an array with a primitive component type and the dest argument refers to an array with a reference component type.
The src argument refers to an array with a reference component type and the dest argument refers to an array with a primitive component type.
Otherwise, if any of the following is true, an IndexOutOfBoundsException is thrown and the destination is not modified:
The srcPos argument is negative.
The destPos argument is negative.
The length argument is negative.
srcPos+length is greater than src.length, the length of the source array.
destPos+length is greater than dest.length, the length of the destination array.
Otherwise, if any actual component of the source array from position srcPos through srcPos+length-1 cannot be converted to the component type of the destination array by assignment conversion, an ArrayStoreException is thrown. In this case, let k be the smallest nonnegative integer less than length such that src[srcPos+k] cannot be converted to the component type of the destination array; when the exception is thrown, source array components from positions srcPos through srcPos+k-1 will already have been copied to destination array positions destPos through destPos+k-1 and no other positions of the destination array will have been modified. (Because of the restrictions already itemized, this paragraph effectively applies only to the situation where both arrays have component types that are reference types.)
パラメーター:
src the source array.
srcPos starting position in the source array.
dest the destination array.
destPos starting position in the destination data.
length the number of array elements to be copied.
スロー:
IndexOutOfBoundsException - if copying would cause access of data outside array bounds.
ArrayStoreException - if an element in the src array could not be stored into the dest array because of a type mismatch.
NullPointerException - if either src or dest is null.
1 楼
vivid_gxp
2009-09-13
兄弟,你是怎么知道她的实现过程的?
据我所知,arraycopy是实现跨平台的。windows系统下是调用某个 dll 的。
而且arraycopy是通过native来声明的,那么就应该是通过C/C++来实现的。那你怎么就能肯定是用了一个临时数组呢?
如果需要将数据拷贝到临时数组,那么性能会很低的。
所以我觉得不应该像楼主所说的那样的。
(ps:(我也不知道具体是怎么实现的,上面的评论只是我的估计。)
据我所知,arraycopy是实现跨平台的。windows系统下是调用某个 dll 的。
而且arraycopy是通过native来声明的,那么就应该是通过C/C++来实现的。那你怎么就能肯定是用了一个临时数组呢?
如果需要将数据拷贝到临时数组,那么性能会很低的。
所以我觉得不应该像楼主所说的那样的。
(ps:(我也不知道具体是怎么实现的,上面的评论只是我的估计。)
发表评论
-
tomcat7下对HTTPS的部署配置
2018-11-09 15:33 7491、申请CA证书,可以在腾讯云或者阿里云申请到免费的CA ... -
IDEA切换 svn 问题
2017-11-27 09:21 4910今天项目,本来以为就几分钟的事没想到,还出现报错了,错误日记 ... -
log4j cfx 接口请求、返回信息 日志文件分离
2017-06-28 15:04 2101log4j.rootLogger=info,stdout, ... -
junit 启动报错:java.lang.NoClassDefFoundError
2013-04-17 19:31 1603junit 启动报错:java.lang.NoClassDe ... -
Java中抽象类和接口的区别
2013-04-16 12:12 990小结 1.abstract class 在 Java ... -
web.xml中servlet, bean, filter, listenr 加载顺序汇总
2013-03-28 16:17 1512web.xml中servlet, bean, filter, ... -
深入Java虚拟机JVM类加载初始化学习笔记
2013-03-26 20:39 12071. Classloader的作用,概括来说就是将编译后的c ... -
http代理出错Proxy Error
2013-03-22 09:15 1609最近项目突然访问几下次报: Proxy Error ... -
通过javamelody监控web应用的性能指标
2013-03-04 12:00 17381、问题背景 为了提升应用的性能我们可能需要做很多分析和 ... -
javamelody监控
2013-03-04 11:50 1481. 将 javamelody-1.42.0.war 文件部署 ... -
myeclipse安装svn插件的肯定可行的方法
2012-12-18 21:26 9201、下载最新的SVN包:http://subclipse.ti ... -
WordTemplate 使用
2012-03-27 11:28 1195http://wiki.softartisans.com/di ... -
asyn4j 异步方法调用框架
2012-03-14 14:19 1923例子: 1.调用普通方法 主方法 ... -
asyn4j -- 异步方法调用框架
2012-03-14 14:15 01.3更新 优化代码 新增任务持久与恢复功能 ... -
JasperReport
2011-09-01 13:45 1242“在JRE 5以上的java环境中,java会自动加载$JAV ... -
解决Linux操作系统下AES解密失败的问题
2011-03-11 16:00 6995现象描述: windows上加解密正常,linu ... -
使用set+list进行过滤重复并排序
2011-02-28 11:30 1796有时要把有重复的数据用set去掉重复元素,但用set后又把元素 ... -
ssh 事务
2011-01-12 23:03 1149ssh在做事务提交时,不能直接用Sql语句进行insert i ... -
struts2与 json
2011-01-10 09:17 1199需要注意的是,如果用JSON插件把返回结果定为JSON。而JS ... -
struts2返回json数据问题
2011-01-06 20:58 1269最近同事反映struts2返回数据不了json数据,特意测试了 ...
相关推荐
### System.arraycopy实现数组之间的复制 在Java编程语言中,`System.arraycopy()`是一个非常实用且高效的方法,用于在数组之间复制元素。此方法属于`java.lang.System`类,并且是一个静态方法,这意味着可以直接...
`System.arraycopy` 和 `Arrays.copyOf` 都是Java中用于复制数组的方法,但它们在使用和处理异常情况上有所不同。这两个方法在处理数组复制时,提供了便利和效率,但各有其适用场景。 `System.arraycopy` 是一个...
使用System.arraycopy()方法可以实现数组的合并,例如: ```java String[] aa = {"11","22","33"}; String[] bb = {"44","55","66"}; String[] cc = {"77","88","99"}; // 合并两个数组 String[] dd = new String...
Java提供了System类的arraycopy()方法,它是进行数组复制的最直接和高效的方式。该方法接受五个参数:源数组、源数组起始位置、目标数组、目标数组起始位置和复制的元素数量。例如: ```java int[] srcArray = {1...
12. **数组复制方法**:除了`=`符号外,还可以使用`System.arraycopy()`方法或`clone()`方法进行数组复制。 13. **数组元素表示**:数组的第三个元素通常表示为`a[2]`,因为下标是从0开始的。 14. **数组下标错误...
- 数组复制:可以使用System.arraycopy()方法或者遍历数组进行复制。 - 排序:使用Arrays.sort()方法对数组进行排序,适用于基本类型的数组;对于对象数组,需要自定义比较器。 - 查找:线性查找效率较低,而二分...
2. 数组复制:可以使用System.arraycopy()方法来复制数组。 3. 数组排序:可以使用Arrays.sort()方法来排序数组。 4. 数组查找:可以使用Arrays.binarySearch()方法来查找某个元素。 关于数组的排序操作 1. 对象...
- `System.arraycopy()`:用于复制数组的一部分到另一个数组。 - `Arrays.equals()`:比较两个数组是否相等(元素相同且顺序一致)。 - `Arrays.sort()`:对数组进行排序。 6. **多维数组** Java支持多维数组,...
16. 错误的说法是A,`=`不能实现数组复制,只能复制引用。 17. 会导致数组越界的是D,循环条件错误,会访问到a[10]。 18. main方法的合法声明是B,缺少void关键字。 这些知识点涵盖了数组的基本操作和特性,是Java...
正确的复制数组的方式是使用`System.arraycopy()`或创建新数组并遍历原数组进行复制。 3. **数组存储位置**:JVM将数组存储在堆内存中,而不是栈内存,因为数组可能很大,栈内存不足以存储大数组。 4. **二分查找*...
2. 使用`System.arraycopy()`复制原数组的元素到新数组,或者通过循环逐个复制。 例如,`ArrayCopy.java`文件可能有以下代码: ```java public class ArrayCopy { public static void main(String[] args) { int...
- `System.arraycopy()`方法用于将一个数组的一部分或全部复制到另一个数组。 - `clone()`方法可以创建数组的浅拷贝。 7. **数组的排序**: - 对于基本类型数组,可以使用Arrays类提供的`sort()`方法进行排序。 ...
11. 数组复制方法除了`arraycopy()`外,还可以使用循环逐个复制,但用`=`赋值只会复制引用。 12. 数组`a`的第三个元素用下标表示为`a[2]`。 13. 访问无效数组下标会抛出`ArrayIndexOutOfBoundsException`异常。 14. ...
1. 使用`System.arraycopy()`:这是一个静态方法,可以从源数组的指定位置开始,将一部分元素复制到目标数组的指定位置。例如: ```java int[] src = {1, 2, 3}; int[] dest = new int[src.length]; System....
2. **数组复制**:在Java中,数组复制通常通过System.arraycopy()方法或使用for循环完成。复制时,实际上是复制数组的引用,而不是复制实际的数据副本。 3. **匿名数组**:没有显式定义变量的数组,可以直接创建并...
我们首先创建了一个新的字符串数组 c,然后使用 System.arraycopy 方法将数组 a 和 b 的元素复制到数组 c 中。 代码实现: ```java public static String[] getThreeArray() { String[] a = {"0", "1", "2"}; ...
// 使用System.arraycopy()复制数组 System.arraycopy(a, 0, c, 0, a.length); System.arraycopy(b, 0, c, a.length, b.length); return c; } ``` ### 合并不同类型的数组 除了以上介绍的字符数组合并外,还...
2. **数组复制**:在Java中,当使用"="操作符复制数组时,实际上是复制了数组的引用,而不是复制数组的内容。这意味着两个数组会指向同一个内存位置,改变其中一个会影响另一个。 3. **匿名数组**:没有显式变量...