◆ When specifying the value of an element
public static void main(String[] args) {
String arr[] = {"orange","apple","cherry","melon","grape"};
List<String> list = new ArrayList<>(Arrays.asList(arr));
list.remove("apple");
}
◆ When specifying the INDEX number of the element
public static void main(String[] args) {
String arr[] = {"orange","apple","cherry","melon","grape"};
List<String> list = new ArrayList<>(Arrays.asList(arr));
list.remove(1);
}
Generate a list with the element you want to remove and put it in the argument of the list.removeAll () method
public static void main(String[] args) {
String arr[] = {"orange","apple","cherry","melon","grape"};
List<String> list = new ArrayList<>(Arrays.asList(arr));
List<String> remove = new ArrayList<>();
Collections.addAll(remove,"apple","melon");
list.removeAll(remove);
System.out.println(list);
}
List.size will be 0 because all are deleted
public static void main(String[] args) {
String arr[] = {"orange","apple","cherry","melon","grape"};
List<String> list = new ArrayList<>(Arrays.asList(arr));
list.clear();
System.out.println(list.size());
}
Recommended Posts