Home Java删除数组中元素的方法
Post
Cancel

Java删除数组中元素的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void main(String[] args) {
    String[] data = new String[] { "a", "b", "c" };
    String del = "c";
    for (int i = 0; i < data.length; i++) {
        if (del.equals(data[i])) {
            for (int k = i; k < data.length - 1; k++) {
                data[k] = data[k + 1];
            }
            data = Arrays.copyOf(data, data.length - 1);
        }
    }
    for (int i = 0; i < data.length; i++) {
        System.out.println(data[i]);
    }
}
This post is licensed under CC BY 4.0 by the author.