案例:数组的扩容与缩容


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));
    }
}

上一篇:PM_11 十大管理之项目风险管理(九)


下一篇:flutter Dialog关闭后刷新