import java.util.Arrays;
public class 数组的扩容与缩容 {
public static void main(String[] args) {
//定义数组
String[] names =
new String[]{"误杀2", "反贪风暴", "羞羞的铁拳", "长津湖", "水门桥", "李茂扮太子"};
//一.数组的扩容
names = Arrays.copyOf(names, names.length + 1);
System.out.println(Arrays.toString(names));
names[names.length - 1] = "唐人街探案3";
// ctrl + d 复制代码
System.out.println(Arrays.toString(names));
//二.数组的缩容
/**
* {"误杀2", "反贪风暴", "羞羞的铁拳", "长津湖", "水门桥", "李茂扮太子","唐人街探案3"};
*/
names = Arrays.copyOf(names, names.length - 1);
System.out.println(Arrays.toString(names));
}
}
案例:数组的扩容与缩容
最新推荐文章于 2024-01-02 09:25:21 发布