简体   繁体   English

java编程死代码

[英]java programming dead code

I just need someone to tell me why index++ is a dead code so I can try to fix it myself.我只需要有人告诉我为什么 index++ 是一个死代码,这样我就可以尝试自己修复它。

heres my code for one class继承人我的一类代码

public class ManagementCompany {
    private String name;
    private String taxID;
    private Property[] properties;
    private double mgmFeePer;
    private final int MAX_PROPERTY = 5;

    public ManagementCompany(String name, String taxID, double mgmFee)
    {
        properties = new Property[MAX_PROPERTY];
        this.name = name;
        this.taxID = taxID;
        this.mgmFeePer = mgmFee;
    }

    public int getMAX_PROPERTY()
    {
        return MAX_PROPERTY;
    }
    public int addProperty(Property property)
    {
        for(int index = 0; index < properties.length; index++)
        {
            properties[index] = property;
                return (index + 1);
        }
        return -1;
    }

heres my other class.这是我的另一堂课。 Not sure if it's needed though虽然不确定是否需要

You have a return in the loop.你在循环中有一个return By unrolling the for you will see why it is dead code:通过展开 for 你会明白为什么它是死代码:

FOR INITIALIZATION: int index = 0;
FOR PRE-LOOP CHECK: index < properties.length

FOR BODY EXECUTION: properties[index] = property;
                    return (index + 1);

FOR POST-LOOP UPDATE: index++

As you can see, the return makes the loop terminate and exit the for() statement and the enclosing method.如您所见, return使循环终止并退出for()语句和封闭方法。 This premature termination of the loop is the cause that execution can never reach the post-loop update index++ .循环的过早终止是执行永远无法到达循环后更新index++

EDIT : I've left this answer incompleted for several hours because the server went under maintenance while I was writing.编辑:我已经将这个答案未完成了几个小时,因为我在写作时服务器正在维护中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM