首页 文章

从通过cmd行给出的args创建新对象会让我从UncaughtExceptionhandler抛出OutOfMemoryError

提问于
浏览
2

我无法找出为什么抛出上述错误 . 当我只输入2个参数(例如22.5公里)时,它运行正常,使用超过(20.5公里400英尺)给出错误 .

这是代码:

if (args.length > 0)
{            
    for (int i = 0; i < args.length; i = +2)
    {
        lengthsCollection.add(new Length(Double.parseDouble(args[i]), args[i+1]));    
    }
}

class Length在其他地方工作正常

public class Length {
    private double valueM;
    private String unitM;

    public Length(double value, String unit)  
    {
        this.valueM = value;
        this.unitM = unit;
    }

有人可以帮我吗?如果用户输入正确(长度单位对)我认为我的代码应该工作

1 回答

  • 1

    这个迭代:

    for (int i = 0; i < args.length; i = +2)
    

    args.length > 2 永远执行,因为 i 永远是 2 . 因此内存错误 .

    你需要的是:

    for (int i = 0; i < args.length; i += 2)
    

相关问题