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

Creating new Object from args given through cmd line gives me OutOfMemoryError thrown from UncaughtExceptionhandler

我不知道为什么会抛出上述错误。当我只输入 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 长度在其他任何地方都工作正常

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

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

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

本次迭代:

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

永远执行,因为 i 永远保持 2。因此内存错误。

您需要的是:

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