public class ArrayTest {
private String[] cc = new String[0];
public void addCC(String ss){
String temp[] = new String[cc.length+1];
for(int i=0;i<cc.length;i++)
temp[i] = cc[i];
temp[cc.length] = ss;
cc = temp;
}
/**
* @param args
*/
public static void main(String[] args) {
ArrayTest sg = new ArrayTest();
sg.addCC("gouhuo");
sg.addCC("xunyou");
sg.addCC("xiayu");
int l = sg.cc.length;
System.out.println(l);
for(int i=0;i<l;i++){
System.out.println(sg.cc[i]);
}
}
}
本文详细阐述了Java中数组动态扩容的过程,通过实例展示了如何在数组长度不足时,创建一个新的数组并复制旧数组元素,从而实现数组的动态增长。
1643

被折叠的 条评论
为什么被折叠?



