1. 数组的拷贝
涉及方法 : System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
参数说明: 表示在数组 src中, 从下标 为srcPos开始的共length 个元素,复制到数组 dest 中,并且是从数组dest下标为destPos的位置开始
测试: 编写代码如下:
static void arraycopy(){
String[] str1 = {"aa","bb","bb","cc","dd"};
String[] str2 = new String[10];
System.arraycopy(str1, 0,str2, 6,3);
for (String s : str2){
System.out.println(s+" ");
}
}
输出如下:
2. 数组的删除
- 数组的删除本质上依旧是数组的复制,测试代码如下:
//测试从数组中删除某个元素(本质上还是数组的拷贝)
public static void testBasicCopy2(){
String[] s1 = {"aa","bb","cc","dd","ee"};
// String[] s2 = new String[5];
System.arraycopy(s1, 3, s1, 3-1, s1.length-3);
s1[s1.length-1] = null;
for(int i=0;i<s1.length;i++) {
System.out.println(i+"--"+s1[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(i+"--"+s[i]);
}
return s;
}
public static void main(String[] args) {
//testBasicCopy2();
String[] str = {"阿里","京东","尚学堂","百度","亚马逊"};
removeElment(str, 1);
// str = extendRange(str);
}
输出结果:
3. 数组的扩容
- 本质上是:先定义一个更大的数组,然后将原数组内容原封不动拷贝到新数组中,测试代码如下:
public static String[] extendRange(String[] s1){
// String[] s1 = {"aa","bb","cc"};
String[] s2 = new String[s1.length+10];
System.arraycopy(s1, 0, s2, 0, s1.length); //就将s1中所有的元素拷贝到了s2
for(String temp:s2){
System.out.println(temp);
}
return s2;
}
public static void main(String[] args) {
String[] str = {"阿里","京东","尚学堂","百度","亚马逊"};
str = extendRange(str);
System.out.println("扩容后数组长度:"+str.length);
}
输出如下: