简体   繁体   中英

Incompatible types error with ArrayList

In my Java class I have this property defined:

private ArrayList requests;

and this is the constructor in the class:

public LeaveRecord() {
    this.requests = new ArrayList<Request>();
    this.daysLeft = ALLOWANCE;
}

in another method I'm trying to return the relevant Request object based on the index of the ArrayList.

public Request getRequestAt(int index) {
    try {
        return this.requests.get(index);
    } catch (IndexOutOfBoundsException e) {
        return null;
    }
}

but I'm getting an error on the return line (line 3 in the above snippet) that says it requires leaverecord.Request but found java.lang.Object .

I don't know what could be wrong, as I defined the ArrayList to be of type Request .

Could someone point me in the right direction? Thanks.

change it to

private ArrayList requests;

private ArrayList<Request> requests

However compiler should have show you the below warning when you say

 private ArrayList requests;

ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized

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