Java – array initializers are not allowed here
•
Java
I have some model classes. I try to declare a list with them, but I get that the array initializer is not allowed here What is a simple job?
... public class M1 extends Model {} public class M2 extends Model {} ... List<Model> mObj = new ArrayList<Model>({M1,M2}) //expression expected ...
Solution
In Java 8, you can use the streams API:
List<String> mObj = Stream.of("m1","m2","m3").collect(Collectors.toList());
Pre Java 8 is easy to use:
List<Model> mObj = new ArrayList<>(Arrays.asList(m1,m2,m3));
For this information, see:
> Collectors > Arrays. asList
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
二维码