package Test;
/**
* System类里也包含了一个static void arraycopy(object src,int srcpos,object dest,int destpos,int length)方法
* 该方法可以将src数组里的元素赋值给dest数组的元素,恰红srcpos指定从src数组的第几个元素开始赋值,length参数指定将src数组的多少
* 个元素赋给dest数组的元素。
* @author pmc
*/
public class ArrayCopy {
public static void main(String[] args){
String[] s1={"aa","bb","cc","DD","EE","FF"};
String[] s2=new String[10];
System.arraycopy(s1, 2, s2, 6, 3);
for(String temp:s2){
System.out.println(temp);
}
System.out.println("------------------");
new ArrayCopy().testBasicCopy();
System.out.println("------------------");
String[] s3={"阿里","京东","腾讯","百度","亚马逊"};
new ArrayCopy().del(s3, 1);
System.out.println("------------------");
addArray();
System.out.println("------------------");
extendRange(s3);
System.out.println("------------------");
String[] s4={"阿里","京东","腾讯","百度","亚马逊"};
addCr(s4,0,"我插入的内容");
}
public void testBasicCopy(){
String[] s1={"aa","bb","cc","DD","EE"};
System.arraycopy(s1, 3, s1, 2, 2);
s1[s1.length-1]=null;
for(String temp:s1){
System.out.println(temp);
}
}
//删除一个
public String[] del(String[] s,int index){
System.arraycopy(s, index+1, s, index, s.length-index-1);
s[s.length-1]=null;
for(String temp:s){
System.out.println(temp);
}
return s;
}
//数组扩容
public static void addArray(){
String[] s1={"1","2","3","4"};
String[] s2=new String[s1.length*2];
for(int i=0;i<s1.length;i++){
s2[i]=s1[i];
}
for(String temp:s2){
System.out.println(temp);
}
}
//数组扩容
public static String[] extendRange(String[] s){
String[] s2=new String[s.length*2];
System.arraycopy(s, 0, s2, 0, s.length);
for(String temp:s2){
System.out.println(temp);
}
return s2;
}
//数组插入
public static String[] addCr(String[] s,int index,String str){
// String[] s1={"1","2","3","4","5","6"};
String[] s2=new String[s.length+1];
s2[index]=str;
System.arraycopy(s, 0, s2, 0, index);
System.arraycopy(s, index, s2, index+1, s.length-index);
for(String temp:s2){
System.out.print(temp+" ");
}
return s;
// System.out.println(s1.length);
// System.out.println(s2.length);
}
}