System.arraycopy(T[] src,T srcBegin,T []des,T desBegin,int Length)
来源:互联网 发布:土猫宠物淘宝怎么样 编辑:程序博客网 时间:2024/04/30 12:14
这个函数主要解释下这几个参数的代表意义,
T []src代表一个源数组,来源;srcBegin代表需要从哪个地方开始复制
T []des代表一个需要复制完之后需要放的位置; desBegin代表这个需要房的起始位置
Length代表的是需要复制的长度;
先来看下代码:
public class TestArrayCopy { /** * @param args */ public static void main(String[] args) { // TODO 自动生成方法存根 String[] s1 = {"中国","山西","太原","TYUT","zyy","加拿大","不知道哪个州","不知道哪个市","不知道哪个学校","yxf"}; String[] s2 = new String[10]; System.arraycopy(s1, 0, s2, 0, 10); s2[6] = "假设蒙大拿州"; s2[7] = "假设蒙特利尔市"; s2[8] = "假设Montreal商学院"; System.out.println("This is s1"); for(int i = 0;i < s1.length ;i++){ System.out.print(s1[i] + ","); } System.out.println("\nThis is s2"); for(int i = 0;i < s2.length ;i++){ System.out.print(s2[i] + ","); } String[][] s3 = {{"中国","山西","太原","TYUT","zyy"},{"加拿大","不知道哪个州","不知道哪个市","不知道哪个学校","yxf"}}; String[][] s4 = new String[s3.length][s3[0].length]; System.arraycopy(s3, 0, s4, 0, s3.length); System.out.println("\nThis is original s3"); for(int i = 0;i < s3.length ;i++){ for(int j = 0; j< s3[0].length ;j++){ System.out.print(s3[i][j] + ","); } } s4[1][1] = "假设蒙大拿州"; s4[1][2] = "假设蒙特利尔市"; s4[1][3] = "假设Montreal商学院"; System.out.println("\nThis is s3 after s4 has changed."); for(int i = 0;i < s3.length ;i++){ for(int j = 0; j< s3[0].length ;j++){ System.out.print(s3[i][j] + ","); } } System.out.println("\nThis is s4"); for(int i = 0;i < s4.length ;i++){ for(int j = 0; j < s4[0].length ; j++){ System.out.print(s4[i][j] + ","); } } } }
这里运行结果:
This is s1
中国,山西,太原,TYUT,zyy,加拿大,不知道哪个州,不知道哪个市,不知道哪个学校,yxf,
This is s2
中国,山西,太原,TYUT,zyy,加拿大,假设蒙大拿州,假设蒙特利尔市,假设Montreal商学院,yxf,
This is original s3
中国,山西,太原,TYUT,zyy,加拿大,不知道哪个州,不知道哪个市,不知道哪个学校,yxf,
This is s3 after s4 has changed.
中国,山西,太原,TYUT,zyy,加拿大,假设蒙大拿州,假设蒙特利尔市,假设Montreal商学院,yxf,
This is s4
中国,山西,太原,TYUT,zyy,加拿大,假设蒙大拿州,假设蒙特利尔市,假设Montreal商学院,yxf,
我在这里要讲最重要的地方是为什么在s4发生变化之后,s3也变了?
这个我已开始也不懂,后来问同学问老师,在网上查资料最后才发现,是是因为这个地方的s3和s4是二维的数组,和一维的数组不一样,一维的数组直接就是通过参数传值过去就行,但是二维不一样,这么理解,二维的数组我们先暂且看成是一维的数组,但是这里边的“元素”都是“一维数组”,懂了没?而这个后边所谓的“一维数组”就是一个元素都是值的数组,也是我们所熟知的数组模型。
这个传过去的是一个一位数组类型的引用,而这个引用会反过来作用于实参,最后在调用他的函数里边的实参也会发生变化,也就是这里边的s3二维数组也发生了变化;
下边再来看下例子:
package Text01;import ioPackage.Cout;public class Exercise_01 {public static int[] copyOf(int[] a, int newLength) { int[] copy = new int[newLength]; System.arraycopy(a, 0, copy, 0, Math.min(a.length, newLength)); return copy; } public static String copyOf(char[] original, int newLength) { String copy = new String(); System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; } public static void main(String[] args) {// TODO Auto-generated method stubString a="abccabcaabddab",b="ab";Cout.addLine(a);Cout.addLine(b);String t=new String(a);a=a.concat(b);Cout.addLine(a);Cout.addLine(t);Cout.addLine(b);int []a1= {1,2,3,4};int []b1=copyOf(a1,a1.length);for(int i:b1) {Cout.noLine(i+" ");}Cout.endl();Cout.endl();Cout.endl();Cout.endl();char []tt=new char[t.length()+b.length()];System.arraycopy(t.toCharArray(),0, tt,0,t.length());System.arraycopy(b.toCharArray(),0, tt,t.length(),b.length());for(char i:tt) {Cout.noLine(i);}Cout.endl();}}
运行结果是什么?
abccabcaabddab
ab
abccabcaabddabab
abccabcaabddab
ab
1 2 3 4
abccabcaabddabab
注明下,这里的Cout是一个输出类,需要自己去写的!!!
我这是写完之后直接就导进去了,做成一个包,然后自己每次编程的时候剩下了不少的时间和精力!!!
- System.arraycopy(T[] src,T srcBegin,T []des,T desBegin,int Length)
- System.Nullable<T> int?
- t
- t
- t
- t
- T
- T
- t
- t
- t
- T
- t
- t
- t
- t
- t
- t
- 51NOD 1127 最短的包含字符串(尺取法)
- java 删除文件夹下的所有文件
- Android5.0以上Button去除阴影
- 编写程序,键盘输入10个数,计算平均值,并打印这10个数、平均值(用数组)
- nginx+tomcat使用apache的FtpClient上传图片时由于多线程问题导致的文件大小为0的问题
- System.arraycopy(T[] src,T srcBegin,T []des,T desBegin,int Length)
- Redis内部数据结构总结(5)skiplist
- java实现实时svm预测
- const,static,extern在iOS开发中的用法心得
- CentOS7 nginx yum 安装及相关命令
- Spring IOC基础 配置文件方式
- jQuery选择器归纳
- .Net面试题中的经典
- velocity之学习