The java.util List add method is used to add an element to a list in Java. It is a part of the java.util package library. The syntax for using the add method is:
list.add(element);
Here, list is an object of the List interface, and element is the element to be added to the list. The add method adds the specified element to the end of the list.
Example 1: Adding an element to the list
List names = new ArrayList<>(); names.add("Alice"); //adds "Alice" to the list
Example 2: Adding multiple elements to the list
List numbers = new LinkedList<>(); numbers.add(1); numbers.add(2); numbers.add(3);
In the above example, we create a LinkedList object called numbers and add three integers to it using the add method.
The java.util package library provides several classes and interfaces for handling lists, such as ArrayList, LinkedList, Vector, and Stack. The add method is available in all of these classes.
Java list.add - 4 examples found. These are the top rated real world Java examples of java.util.list.add extracted from open source projects. You can rate examples to help us improve the quality of examples.