简体   繁体   中英

Not matching type of LIst<> and its element - Java

I have a list of types DataRespose . I am using it to get a response from an external API and then to test it.

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`

I am aware that type is no the same, but then what should I add to be type DataResponse

DataResponse.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.

  List<DataResponse> dataResponseList = new ArrayList<>();
    dataResponseList.add(new DataResponse("data"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM