Why in Java util. Static Java. Java is declared in collections util. Collections. Fill () method instead of Java util. Instance method in abstractlist?

In Java util. In collections, there is a method:

public static <T> void fill(List<? super T> list,T obj)

Populates the list specified by the first parameter with the object specified by the second parameter To call this method, you can write it once, for example:

java.util.List<String> l;
//...
java.util.Collections.fill(l,"Hello");

Why is this method not in Java util. Declared as instance method in abstractlist? If so, it can be used as an instance method and called more convenient:

java.util.List<String> l;
//...
l.fill("Hello");

Solution

Because there are classes other than abstractlist to implement the interface list An example of a Java library is copyonwritearraylist, which can be implemented by anyone Therefore, by accepting list, this method is more useful because it can be used with any of these classes

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>