The Java list ` of ‘method is very confusing

When I look at a Java document

It is very overloaded, and the number of all elements ranges from 1 to 10

It says:

I don't understand this at all, and can I find a way to use it?

Solution

List. The of () method is a convenient static helper that can create a fixed size list in one call (instead of creating an empty list and calling it several times) It happened that this @ r_ 301_ 1819 @ list is not modifiable (no elements can be added or deleted)

For example, compare:

ArrayList<String> supportedLanguages = new ArrayList<>();
supportedLanguages.add("en-us");
supportedLanguages.add("en-gb");
supportedLanguages.add("de");

To:

List<String> supportedLanguages = List.of("en-us","en-gb","de");

There happen to be 11 such overloads, ranging from zero to ten elements (list. Of() becomes an empty unmodifiable list), and vararg overloads with the signature @ safevarargs static < E > are used to handle larger situations List and lt e – substitute; (E... Element)

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
分享
二维码
< <上一篇
下一篇>>