我们如何在不使用remove()
java 中的方法的情况下从列表中删除元素?使用集合概念。
通常我知道删除元素使用remove()
import java.util.ArrayList;
public class ArrayListDemo {
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");
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 element "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);
}
}
}