1. System.arraycopy()方法
import java.util.Arrays;
public class ArrayCopy {
public static void copyTest(int a[],int starta,int b[],int startb,int copylength){
//第一步,检查原数组a是否为空,以及原数组a是否有足够的用于复制的元素。
if(a == null){
System.out.println("空指针异常,原数组为空。");
return;
}else{
if(starta+copylength > a.length){
System.out.println("数组越界异常,原数组中元素不够用来进行复制。");
return;
}
}
//第二步,检测目标数组是否为空,以及目标数组是否有足够的空间用于存放复制的元素
if(b == null){
System.out.println("空指针异常,目标数组为空。");
return;
}else{
if(startb+copylength > b.length){
System.out.println("数组越界异常,目标数组没有足够的空间用于存放所复制的元素。");
return;
}
}
//进行复制
for(int i=1;i<=copylength;i++){
b[startb] = a[starta];
starta++;
startb++;
}
}
public static void main(String args[]){
//数组的复制方法一
int[] a = {1,2,3,4,5,6};
int[] b = new int[a.length];
System.out.println("a:"+Arrays.toString(a));
/*
* 1.采用System.arraycopy()方法
* 第一个参数,原数组
* 第二个参数,从原数组复制的元素的起始位置
* 第三个参数,复制的目标数组
* 第四个参数,复制的目标数组的元素的起始位置
* 第五个参数,从原数组复制元素的个数
*/
System.arraycopy(a, 0, b, 2, 3);
System.out.println("b:"+Arrays.toString(b));
/*
* 2.运用自定义的copyTest()函数,参数同上
*/
int[] c = new int[a.length];
copyTest(a, 0, c, 2, 3);
System.out.println("c:"+Arrays.toString(c));
}
}
2. Arrays.copyof()方法
import java.util.Arrays;
public class ArrayCopyOf {
public static int[] copyofTest(int a[],int length){
//检查参数的合法性
//1.检查数组a 是否为空
if(a == null){
System.out.println("原数组为空,产生空指针异常!");
return null;
}
//2.检查lengths是否合法
if(length < 0){
System.out.println("length参数不合法!");
return null;
}
//新建一个长度length的数组用于存放
int[] temp = new int[length];
/*
* 对数组temp中元素进行赋值
* 1. 如果要复制的元素的个数超过了原数组中元素的个数,超过的部分设为数组类型的默认值
* 2. 如果要复制的元素的个数没有超过原数组中元素的个数,复制所需个数即可
*/
if(a.length < length){
for(int i=0;i<a.length;i++){
temp[i] = a[i];
}
}else{
for(int i=0;i<length;i++){
temp[i] = a[i];
}
}
return temp;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//数组的复制方法二
int[] a = {1,2,3,4,5,6};
int[] b = null;
System.out.println("a:"+Arrays.toString(a));
/*
* 1.采用Arrays类的copyof()方法,返回一个新的指定长度的数组对象
* 第一个参数 原数组名
* 第二个参数 从原数组中复制元素的个数,不能为负数,超过原数组长度,用复制元素为数组类型的默认值代替
*/
//目标数组b可以不指定长度,因为Arrays.copyOf(a, 2*a.length) 返回的是一个数组对象
b = Arrays.copyOf(a, 2*a.length);
System.out.println("b:"+Arrays.toString(b));
int[] c = null;
/*
* 2.采用自定义copyofTest()方法,参数同上
*/
c = copyofTest(a,2*a.length);
System.out.println("c:"+Arrays.toString(c));
}
}
3. 采用System.arraycopy和Arrays.copyof方法实现数组的插入
import java.util.Arrays;
public class ArrayInsert {
/*
* 功能:在数组的指定位置插入元素
* a 要插入元素的数组
* pos 新插入的元素的位置
* b 新插入的元素值
*/
public static int[] Insert(int a[],int pos,int b){
//1.检查a是否为空
if(a == null){
System.out.println("要插入的数组不能为空!");
return null;
}
//2.检查插入位置的合法性
if(pos<0 || pos>a.length){
System.out.println("插入位置不合法");
return a;
}
/*
* 第一种方法,采用System.arraycopy()方法
* 新建一个数组,存放插入元素后数组中元素的值
*/
/*
int[] temp = new int[a.length+1];
System.arraycopy(a, 0, temp, 0, pos);
temp[pos] = b;
System.arraycopy(a, pos, temp, pos+1, a.length-pos);
*/
/*
* 第二种方法,采用Arrays.copyOf()方法进行复制,然后在进行移位
*/
int[] temp = Arrays.copyOf(a, a.length+1);
for(int i=temp.length-1;i>pos;i--){
temp[i] = temp[i-1];
}
temp[pos] = b;
return temp;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {1,2,3,4,5,6};
System.out.println("a:"+Arrays.toString(a));
a = Insert(a,0,7);
System.out.println("a:"+Arrays.toString(a));
}
}