数组的扩容
方法: 新建一个长度大的新数组newArr,将原数组arr的元素复制到newArr。再将newArr的地址复制给原数组arr。就达到了数组扩容的目的
public class Test01{
public static void main(String [] args){
String [] arr= {"shangsan","lisi","wangmazi"};
//计算容量:按照1.5倍来算
int capacity = arr.length +(arr.length/2);
String [] newArr = new String [capacity];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}
//将newArr地址复制给arr
arr = newArr ;
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+"\t");
}
}
}
输出