Java List. Remove() method: removes the specified element from the list
•
Java
Grammar 1
remove(int index)
Example
public static void main(String[] args){
List<String>list = new ArrayList<String>();
list.add("苹果"); //向列表中添加数据
list.add("草莓"); //向列表中添加数据
list.add("香蕉"); //向列表中添加数据
String str = list.remove(1);/ /移除索引位置为1的元素
System.out.println("我不爱吃的水果是:"+str);
Iterator it = list.iterator(); //获取集合的Iterator对象
System.out.println("爱吃的水果是:");
while(it.hasNext()){ //遍历Iterator对象
System.out.println(it.next()); //输出Iterator对象中元素
}
}
Grammar 2
remove(Object o)
Example
public static void main(String[] args){
List<String>list = new ArrayList<String>();
list.add("保护环境"); //向列表中添加数据
list.add("爱护地球"); //向列表中添加数据
list.add("从我做起"); //向列表中添加数据
boolean ret = list.remove("从我做起"); //移除指定元素
if(ret){
System.out.println("元素被移除成功");
}else{
System.out.println("列表中不包含此元素");
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
