[英]How can I create an ImmutableList as a member variable?
I need to create an ImmutableList
as a member variable.我需要创建一个ImmutableList
作为成员变量。
Here's what I tried:这是我尝试过的:
private List<String> stringList = Arrays.asList("a", "b", "c");
private ImmutableList<String> stringList2 = Collections.unmodifiableList(stringList);
This fails to compile with the error:这无法编译并出现错误:
FakeRemoteDataStore.java:59: error: incompatible types: no instance(s) of type variable(s) T exist so that List<T> conforms to ImmutableList<String>
private ImmutableList<String> stringList2 = Collections.unmodifiableList(stringList);
^
where T is a type-variable:
T extends Object declared in method <T>unmodifiableList(List<? extends T>)
How can I create an ImmutableList
as a member variable?如何创建一个ImmutableList
作为成员变量?
ImmutableList
is part of Guava, so you can just do: ImmutableList
是番石榴的一部分,所以你可以这样做:
private ImmutableList<String> stringList = ImmutableList.of("a", "b", "c");
You can use copyOf
function from ImmutableList
您可以使用ImmutableList
中的copyOf
function
ImmutableList<String> stringList2 = ImmutableList.copyOf(stringList);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.