VOOZH about

URL: https://www.geeksforgeeks.org/java/list-clear-method-in-java-with-examples/

⇱ List clear() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

List clear() method in Java with Examples

Last Updated : 29 Nov, 2024

The clear() method of List interface in Java is used to remove all of the elements from the List container. This method does not deleted the List container, instead it just removes all of the elements from the List. 

Example:


Output
Original : [1, 2]
After Operation : []


Syntax of Method

public void clear()

Parameter: This method accepts does not accepts any parameter.

Return Value: The return type of the function is void and it does not returns anything. 

Exceptions: This method throws an UnsupportedOperationException if the clear() operation is not supported by this list. 

Example of List.clear() method 

Program 1:


Output
Original : [Geeks, For, Geeks]
After Operation : []

Program 2:


Output
Original : [10, 20, 30]
After Operation : []

Reference:https://docs.oracle.com/javase/7/docs/api/java/util/List.html#clear()

Comment