The following code gives a compile error:
public void method(List<String> aList) {}
public void passEmptyList() {
method(Collections.emptyList());
}
Is there a way to pass an empty list to method
without
new ArrayList<String>()
?
The isEmpty() method of List interface in java is used to check if a list is empty or not. It returns true if the list contains no elements otherwise it returns false if the list contains any element.
To represent an Empty List in Java (an ArrayList is a List), we use java. util. Collections. emptyList() .
motif=new ArrayList(); will make the passed arraylist motif to be new instance of arraylist, and thus since its new , its empty. I have not use motif or number. Mot is string arraylist in another class (NewJFrame). When I call it in another class i-e with j.
You can specify the type param like so:
public void passEmptyList() {
method(Collections.<String>emptyList());
}
Replace
method(Collections.emptyList());
with
method(Collections.<String>emptyList());
The <String>
after the .
is an explicit binding for emptyList
's type parameter, so it will return a List<String>
instead of a List<Object>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With