package cn.zxg.arrays2;
/**
* 测试数组的拷贝
*/
public class TestArrayCopy {
public static void main(String[] args) {
TestBaseCopy();
TestBaseCopy2();
String [] str={"阿里","京东","百度","亚马逊"};
removeElment(str,1);
}
//数组的拷贝
public static void TestBaseCopy(){
String [] str1={"aa","bb","cc","dd","ee"};
String [] str2=new String[10];
//从str1下标为2的位置复制3个到str2下标为4开始的位置
System.arraycopy(str1,2,str2,4,3);
for (String a:str2) {
System.out.println(a);
}
}
//数组元素的删除(其实也是数组的拷贝)
public static void TestBaseCopy2(){
String [] str1={"aa","bb","cc","dd","ee"};
// String [] str2=new String[];
System.arraycopy(str1,3+1,str1,3, str1.length-3-1);
str1[str1.length-1]=null;
for (String i:str1
) {
System.out.println(i);
}
}
//删除数组中指定的索引位置的元素,并将原数组返回
public static String[] removeElment(String[] s,int index){
System.arraycopy(s,index+1,s,index,s.length-index-1);
s[s.length-1]=null;
for (int i=0;i<s.length;i++){
System.out.println(s[i]);
}
return s;
}
//数组的扩容(本质:先定义一个更大的数组,将原数组原封不动拷贝到新数组中)
public static void extendRange(){
String[] str1={"aa","bb","cc"};
String[] str2=new String[str1.length+10];
System.arraycopy(str1,0,str2,0,str1.length);
for (String temp:str2){
System.out.println(temp);
}
}
}