System.arraycopy(T[] src,T srcBegin,T []des,T desBegin,int Length)

这个函数主要解释下这几个参数的代表意义,

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 stub
		String 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是一个输出类,需要自己去写的!!!

我这是写完之后直接就导进去了,做成一个包,然后自己每次编程的时候剩下了不少的时间和精力!!!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值