VOOZH about

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

⇱ List add(E ele) method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

List add(E ele) method in Java with Examples

Last Updated : 11 Dec, 2018
The add(E ele) method of List interface in Java is used to insert the specified element to the end of the current list. Syntax:
public boolean add(E ele)
Parameter: This method accepts a single parameter ele which represents the element to be inserted at the end of this list. Return Value: The function returns a boolean value True if the element is successfully inserted in the List otherwise it returns False. Exceptions:
  • UnsupportedOperationException - It throws this exception if the add() operation is not supported by this list.
  • ClassCastException - It throws this exception if the class of the specified element prevents it from being added to this list.
  • NullPointerException - It throws this exception if the specified element is null and this list does not permit null elements.
  • IllegalArgumentException - It throws this exception if some property of this element prevents it from being added to this list.
Below programs illustrate the RoleList.add(Object obj) method: Program 1:
Output:
Number = 15
Number = 20
Number = 25
Program 2:
Comment