[英]Not matching type of LIst<> and its element - Java
I have a list of types DataRespose
.我有一个类型列表
DataRespose
。 I am using it to get a response from an external API and then to test it.我正在使用它从外部 API 获得响应,然后对其进行测试。
I am trying to create a new list with the same type and add a new element to it but I don't know-how.我正在尝试创建一个具有相同类型的新列表并向其中添加一个新元素,但我不知道如何操作。
I tried something like this:我试过这样的事情:
List<DataResponse> dataResponseList = new ArrayList<>();
dataResponseList.add("dataId");
But I am getting error of:但我收到以下错误:
Required type: DataResponse Provided: String`所需类型:DataResponse 提供:String`
I am aware that type is no the same, but then what should I add to be type DataResponse
我知道类型不一样,但是我应该添加什么才能成为
DataResponse
类型
DataResponse.java数据响应.java
public class DataResponse {
public final String dataId;
public DataResponse(String dataId) {
this.dataId = dataId;
}
}
List<DataResponse> dataResponseList = new ArrayList<>();
dataResponseList.add(new DataResponse("dataId"));
The list expects an object of DataResponse type.该列表需要 DataResponse 类型的 object。
List<DataResponse> dataResponseList = new ArrayList<>();
dataResponseList.add(new DataResponse("data"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.