Java ArrayList remove()方法

java.util.ArrayList.remove(Object) 该方法用于从ArrayList删除指定元素。

1 语法

public boolean remove(Object o)

2 参数

o:要从ArrayList中删除的元素(如果存在)。

3 返回值

如果ArrayList包含指定的元素,则此方法返回true,否则ArrayList保持不变。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.ArrayList.remove(Object)方法的例子
 */
import java.util.ArrayList;

public class Demo {
    public static void main(String[] args) {

        // create an empty array list with an initial capacity
        ArrayList<String> arrlist = new ArrayList<String>(5);

        // use add() method to add values in the list
        arrlist.add("G");
        arrlist.add("E");
        arrlist.add("F");
        arrlist.add("M");
        arrlist.add("E");

        System.out.println("Size of list: " + arrlist.size());

        // let us print all the values available in list
        for (String value : arrlist) {
            System.out.println("Value = " + value);
        }

        // Removes first occurrence of "E"
        arrlist.remove("E");

        System.out.println("Now, Size of list: " + arrlist.size());

        // let us print all the values available in list
        for (String value : arrlist) {
            System.out.println("Value = " + value);
        }
    }
}

输出结果为:

Size of list: 5
Value = G
Value = E
Value = F
Value = M
Value = E
Now, Size of list: 4
Value = G
Value = F
Value = M
Value = E

 

热门文章

优秀文章