简体   繁体   English

Java类型转换列表

[英]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. 我对Java泛型的工作方式有些困惑,希望有人可以帮助我更好地理解。

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. 我收到一个TypeSafety警告,说未经检查的演员。

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 但是我似乎无法获得像这样的List的类对象

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. 这里的“魔术”是具有通用类Pagination ,其中T是整个类中相同的类型参数。

And here's how to instantiate it (mind the diamond operator <> which was introduced in Java 7 and helps to reduce redundant information): 以及如何实例化它(注意Java 7中引入的菱形运算符<> ,它有助于减少冗余信息):

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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