To create a new empty list in Java, you can use the new
operator to create an instance of a concrete implementation of the List
interface, such as ArrayList
or LinkedList
.
Here is an example of how to create a new empty ArrayList
:
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<String> list = new ArrayList<>(); System.out.println(list); // prints [] } }
This creates a new empty ArrayList
of strings. You can add elements to the list using the add
method.
You can also use the Collections.emptyList
method from the java.util
package to create an empty list. For example:
import java.util.List; import java.util.Collections; public class Main { public static void main(String[] args) { List<String> list = Collections.emptyList(); System.out.println(list); // prints [] } }
This creates an empty list that cannot be modified.