1

evaluate()我使用来自 apache 的 Jexl lib,并且在使用Expression 类的方法时遇到了一些问题。这是 NelderMead 类的代码:

import org.apache.commons.jexl2.*;

public class NelderMead {
    // контсанты
    private static int      M = 3;
    private static double   E = 0.005;
    private static double   A = 1.000;
    private static double   B = 0.500;
    private static double   Y = 2.000;

    // переменные
    private JexlEngine jexl;
    private Expression func;
    private String funcString = "";
    private MapContext[] iterations;

    public NelderMead(){
        this.jexl = new JexlEngine();
    }

    public NelderMead(String funcString){
        this.jexl = new JexlEngine();
        this.setFunc(funcString);
    }


    public void setFunc(String funcString){
        this.funcString = funcString;
        this.func = this.jexl.createExpression(funcString);
    }

    public double funcEval(MapContext args){
    return ((Double) this.func.evaluate(args)).doubleValue();

    }

    public boolean checkCriterian(){
        return true;
    }
}

测试用例的代码是:

import org.apache.commons.jexl2.MapContext;


public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        NelderMead nm = new NelderMead("(x1-2)^4+(x1-2*x2)^2");
        MapContext mc = new MapContext();
        mc.set("x1", 2);
        mc.set("x2", 1);
        System.out.println(nm.funcEval(mc));

    }

}

当我运行测试用例时,会导致以下错误:

Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double
    at NelderMead.funcEval(NelderMead.java:33)
    at Test.main(Test.java:14)

我不明白为什么它不能转换为 Double?

附言

是evaluate() 函数的javadoc。

4

2 回答 2

1

这个测试用例应该模仿你的问题

package com.sg2net.test;

public class TestCast {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TestCast tc= new TestCast();
        tc.funcEval();
    }

    public double funcEval(){
        return ((Long) eval()).doubleValue();
    }   

    private Object eval() {
        return new Long(1);
    }

}

它运行没有问题。您发布的代码是给您异常的代码吗?

这是您使用 Long 修改的代码

import org.apache.commons.jexl2.*;

public class NelderMead {
    // контсанты
    private static int      M = 3;
    private static double   E = 0.005;
    private static double   A = 1.000;
    private static double   B = 0.500;
    private static double   Y = 2.000;

    // переменные
    private JexlEngine jexl;
    private Expression func;
    private String funcString = "";
    private MapContext[] iterations;

    public NelderMead(){
        this.jexl = new JexlEngine();
    }

    public NelderMead(String funcString){
        this.jexl = new JexlEngine();
        this.setFunc(funcString);
    }


    public void setFunc(String funcString){
        this.funcString = funcString;
        this.func = this.jexl.createExpression(funcString);
    }

    public double funcEval(MapContext args){
    return ((Long) this.func.evaluate(args)).doubleValue();

    }

    public boolean checkCriterian(){
        return true;
    }
}

它运行没有问题。评估函数返回一个 Long 对象。由于 Object 是 Java 中的根类,evaluate 函数可以返回任何类。

于 2011-11-26T13:12:11.920 回答
0

方法的返回类型是为什么要将语句中double的结果转换为?returnLong

于 2011-11-26T13:01:52.273 回答