java.math.BigDecimal 不能转换为 [Ljava.lang.Object;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16355690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:28:25  来源:igfitidea点击:

java.math.BigDecimal cannot be cast to [Ljava.lang.Object;

javaspringspring-mvc

提问by Dev

List queryList = executeReadAllSQLQuery(queryString);
    for (Iterator i = queryList.iterator(); i.hasNext();) {
        Object values[] = (Object[]) i.next();
        FDetails pDetails = transform(values);
        fDList.add(pDetails);
        values = null;
    }

Error I am getting at line 3 : java.math.BigDecimal cannot be cast to [Ljava.lang.Object;

我在第 3 行遇到的错误: java.math.BigDecimal cannot be cast to [Ljava.lang.Object;

My transform function :

我的转换功能:

private FDetails transform(Object[] values) {
    FDetails Details = new FDetails();
    Details.setPb((BigDecimal)values[0]);
    Details.setPm((BigDecimal)values[1]);
    Details.setEl((BigDecimal)values[1]);
    Details.setUl((BigDecimal)values[1]);
    return BalanceDetails;
}

Please help me resolve these issue.

请帮我解决这些问题。

采纳答案by Aaron Digulla

How about this code:

这段代码怎么样:

@SuppressWarnings("rawtypes")
List<BigDecimal> queryList = executeReadAllSQLQuery(queryString);

FDetails details = new FDetails();
int i = 0;
details.setPb(queryList.get(i ++));
details.setPm(queryList.get(i ++));
...

fDList.add(pDetails);

Note: Calling fDList.add()inside of the loop is almost certainly wrong. The loop gets one value from the list but you want all five values from the list, create oneinstance of FDetailsfrom those five values and add that single instance to fDListonce

注意:fDList.add()在循环内部调用几乎肯定是错误的。循环从列表中获取一个值,但您想要列表中的所有五个值,从这五个值中创建一个实例FDetails并将该单个实例添加到fDList一次

回答by NINCOMPOOP

The below line is returning you a java.math.BigDecimalwhich you are trying to cast illegaly to Object[]. It seems yourqueryListis a List<java.math.BigDecimal>.

下面的行将返回您java.math.BigDecimal试图非法转换的 a Object[]。看来你的queryList是一个List<java.math.BigDecimal>.

i.next(); // is returning you a  java.math.BigDecimal

回答by Evgeniy Dorofeev

It's clear from error that your List queryList is in fact a list of BigDecimals. So this would be work

从错误中可以清楚地看出,您的 List queryList 实际上是一个 BigDecimals 列表。所以这将是工作

BigDecimal value = (BigDecimal) i.next();

but since it's not what you expect then executeReadAllSQLQuery returns wrong result

但由于它不是您所期望的,因此 executeReadAllSQLQuery 返回错误的结果

BTW for-each would look better anyway

BTW for-each 反正会更好看

for (Object obj : queryList) {
   ...

回答by Rajkumar Teckraft

This works very fine!. Please try this

这很好用!请试试这个

    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("amt", "1000");

    System.out.println("Amount is::"
            + new BigDecimal((String) map.get("amt")));