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();
<T> List<T> getPagedList() throws Exception;and calling it withList<Trade> trades = getPagedList()List<?>as a list whose elements are of some random unknown type.Listyou return actually containsTradevalues?