Why doesn’t Java recognize my ArrayList with an overloaded constructor
•
Java
I have two constructors set as follows:
public XMessage(Information info,List<Object> results) { this.information = info; this.results = results; } public XMessage(Information info,Object result) { this(info,Collections.singletonList(result)); }
I create an xmessage object by passing in an information object and an ArrayList object When I check the result, it is a singleton list that wraps the ArrayList item Why doesn't Java use a more appropriate constructor and what are the options to force it?
Solution
Call the first constructor:
new XMessage(information,new ArrayList<Object>());
Call the second constructor:
new XMessage(information,new ArrayList<String>());
ArrayList < string > () is not treated as list < Object > and ArrayList < Object > is Consider using the following constructor:
public XMessage(Information info,List<? extends Object> results)
As @ luiggi Mendoza suggested below
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
二维码