String java.util.List.remove(int index)
Removes the element at the specified position in this list
boolean java.util.List.remove(Object o)
Removes the first occurrence of the specified element from this list,
/**
* @author lautturi.com
* Java example: remove element from list in java
*/
public class Lautturi {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("lautturi");
list.add("java");
list.add("python");
list.add("hello");
list.add("js");
System.out.println("list:"+list);
String removedElement = list.remove(2);
System.out.println(removedElement);
System.out.println("list:"+list);
// remove by element
boolean result = list.remove("hello");
System.out.println(result);
System.out.println("list:"+list);
}
}
output:
list:[lautturi, java, python, hello, js] python list:[lautturi, java, hello, js] true list:[lautturi, java, js]