简体   繁体   中英

Java Type Casting List

I am a little confused on how the Generics of Java work and am hoping someone can help me understand a little better.

I am calling a method from another class.... Here is the method that I am calling.

public List<?> getPagedList() throws Exception;

When I call this method like so

myList = (List<Trade>) getPagedList();

I get a TypeSafety warning saying unchecked cast.

I tried changing the method to this

<T> T getPagedList(Class<T> myClass) throws Exception;

But I cannot seem to get the class object of List like this

getPagedList((List<Trade>).class

Any ideas or direction I can start learning?

EDIT ---- The class

public class Pagination{
    private static final int MAX_PAGE_LENGTH = 20;
    private static final int MAX_PAGES = 5;

    private int currentPage;
    private List list;

    public Pagination(List<?> list, String currentPage){
        this.list = list;

        if(currentPage == null)
            this.currentPage = 1;
        else
            this.currentPage = Integer.parseInt(currentPage);
    }

    public <T> List<T> getPagedList() throws Exception{
        if(currentPage * MAX_PAGE_LENGTH + MAX_PAGE_LENGTH > list.size()){
            return list.subList(currentPage*MAX_PAGE_LENGTH, list.size());
        }else{
            return list.subList(currentPage * MAX_PAGE_LENGTH, currentPage * MAX_PAGE_LENGTH + MAX_PAGE_LENGTH);
        }
    }
}

My Call

    List<Trade> ts = (Some Code to put objects in ts)
    Pagination paging = new Pagination(ts, currentPage);
    List<Trade> ts = paging.getPagedList();

No need to pass a parameter:

public class Pagination<T> {

    private static final int MAX_PAGE_LENGTH = 20;
    private static final int MAX_PAGES = 5;

    private int currentPage;
    private List<T> list;

    public Pagination(List<T> list, String currentPage){
        this.list = list;

        if (currentPage == null)
            this.currentPage = 1;
        else
            this.currentPage = Integer.parseInt(currentPage);
    }

    public List<T> getPagedList() throws Exception {
        if (currentPage * MAX_PAGE_LENGTH + MAX_PAGE_LENGTH > list.size()){
            return list.subList(currentPage * MAX_PAGE_LENGTH, list.size());
        }
        return list.subList(currentPage * MAX_PAGE_LENGTH, currentPage * MAX_PAGE_LENGTH + MAX_PAGE_LENGTH);
    }
}

Isn't that what you want? The "magic" here is to have a generic class Pagination where T is the same type parameter throughout the whole class.

And here's how to instantiate it (mind the diamond operator <> which was introduced in Java 7 and helps to reduce redundant information):

Pagination<Trade> p = new Pagination<>(myListOfTrades, null);

You can do this

<T> List<T> getPagedList(Class<T> myClass) throws Exception;

This means you can pass the type of the element as an argument.

To expand on my comment, you can use this as the signature of the method:

<T> List<T> getPagedList(Class<T> type) throws Exception;

And call it like this:

List<Trade> trades = getPagedList(Trade.class);

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