删除数组中指定的元素,然后将后面的元素向前移动一位,将最后一位设置为NULL 。 String[] strs={“aaa”,”ccc”,”ddd”,”eee”,”fff”,”ggg”}; 指定删除字符串“ccc”,把后的元素依次向前移动!!!

public static void main(String[] args) {
        int temp = -1;
        String[] strs = {"aaa", "ccc", "ddd", "eee", "fff", "ggg"};
        for (int i = 0; i < strs.length - 1; i++) {
            if (strs[i].equals("ccc")) {
                temp = i;
            }
            if (temp != -1) {
                strs[i] = strs[++temp];
            }
            if (strs.length - 1 == temp) {
                strs[temp] = null;
            }
        }
        for (String q : strs) {
            System.out.print(q + " ");
        }
    }
posted @ 2020-11-29 22:09  MikiKawai  阅读(365)  评论(1编辑  收藏  举报