This method are used to remove all elements from the invoking collection.
Collection Interface clear() Method:
This method are used to remove all elements from the invoking collection.
Syntax :
void clear();
Example :
import java.util.*;
public class CollectionInterfaceClearMethodExample {
public static void main(String[] args) {
Collection collection = new ArrayList();
collection.add(1);
collection.add(2);
collection.add(3);
System.out.print("Elements of the Array List before use of clear method ");
System.out.println(collection + ".");
collection.clear();
System.out.print("Elements of the Array List after use of clear method ");
System.out.println(collection + ".");
}
}
Output :
|
Elements of the Array List before use of clear method [1, 2, 3]. Elements of the Array List after use of clear method []. |

[ 0 ] Comments