简体   繁体   中英

How to convert List<?> to List<className>

I am facing some problem when converting list to List

List<ContentDes> contentDes_ls  = new ArrayList<ContentDes>();
        logger.info("in getContentDes ");

        List<?> ls  = ho.getResultListByLimit(sql,limit);
        contentDes_ls = (List<ContentDes>)ls;
    logger.info(" size of content "+contentDes_ls.size());
    for (ContentDes contentDes : contentDes_ls) {
        logger.info(contentDes.getPricetag());
        logger.info(contentDes.getPrv());
    }

Its worked fine when I get the size of List<SomeClass> but when I access the getter and setter of SomeClass I got exception

Output:

size of content 2

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.SomeClass] 

SomeClass is not mentioned anywhere in your code, so I assume you mean ContentDes.

It appears your list doesn't contain ContentDes instances. The Exception indicates the items are instead of type Object[] . This causes a ClassCastException when you try to iterate over the items as ContentDes.

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