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);
}
}
}