您的位置:首页 > 编程语言 > Java开发

java使用System.arraycopy对数组扩容

2017-09-19 17:24 381 查看
使用System.arraycopy,把原数组复制进新的数组,System.arraycopy接口文档。

/**
*
* @param src
需要复制的数组
* @param srcPos
从原始数组的哪个下标开始复制
* @param dest
复制的新的数组
* @param destPos
*        从新数组的哪位开始复制
* @param length
*        复制原始数组的长度
*/
arraycopy(Object src,  int  srcPos, Object dest, int destPos,int length)


方法示例:

public class EnlargeArray {
/**
*对数组进行扩容
*/
public static void main(String[] args){
//原始数组
int[] originArray=new int[2];
originArray[0]=11;
originArray[1]=22;

//下面进行扩容
int[] newArray=new int[originArray.length*2];
System.arraycopy(originArray, 0, newArray, 0, originArray.length);
for(int i:newArray){
System.out.println(i);
}
}
}

输出:

0
11
22
0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java