没有参数访问列表中的下一个元素

时间:2018-10-28 14:58:42

标签: java arraylist

作为练习,我已经掌握了一些功能的核心,而我必须实现那些遗漏的功能。 我们正在研究调度程序和操作:

Action类:

public abstract class Action {
    private ActionState state;


public Action() {
    this.state = ActionState.READY;
}

/**
 * make one step if the action is in state READY
 * @throws ActionFinishedException if the state is FINISHED
 */
public void doStep() throws ActionFinishedException{
    if (this.isFinished()) {
        throw new ActionFinishedException("Action is finished");
    }
    if (this.state == ActionState.READY) {
        this.state = ActionState.IN_PROGRESS;
    }
    this.makeOneStep();
    if (this.stopCondition()) {
        this.state = ActionState.FINISHED;
    }
}

protected abstract void makeOneStep() throws ActionFinishedException;
protected abstract boolean stopCondition();

/**
 * @return the state
 */
protected ActionState getState() {
    return this.state;
}

/**
 * @return true if the state is FINISHED, false otherwise
 */
public boolean isFinished() {
    return this.state == ActionState.FINISHED;
}

}

Scheduler类:

public abstract class Scheduler extends Action {
protected List<Action> theActions;

public Scheduler() {
    this.theActions = new ArrayList<Action>();
}


@Override
protected void makeOneStep() throws ActionFinishedException {
    Action action = this.nextAction();
    action.doStep();
    if (action.isFinished()) {
        this.removeAction(action);
    }
}

protected List<Action> actions() {
    return this.theActions;
}

public abstract void removeAction(Action action);

protected abstract Action nextAction();

public void addAction(Action action) throws ActionFinishedException, SchedulerStartedException {
    if (this.getState() != ActionState.READY) {
        throw new SchedulerStartedException("Can't add when scheduler is in progress");
    }
    if (action.isFinished()) {
        throw new ActionFinishedException("Can't add an already finished action");
    } else {
        this.theActions.add(action);
    }
}

@Override
protected boolean stopCondition() {
    return this.theActions.isEmpty();
}

}

我在实现nextAction()时遇到了麻烦,因为给定的签名没有任何参数,我无法使用.get(index+1)访问下一个元素,为此创建迭代器似乎很多小任务

我正在nextAction()类中实现fairScheduler

public class FairScheduler extends Scheduler {

    @Override
    /** removes a given action from the scheduler
     * @param action the action to remove
     */
    public void removeAction(Action action) {
        this.theActions.remove(action);
    }

    /** returns the nextAction in the scheduler, 
     * if the current action is the last element of the scheduler
     * the first action of the scheduler is returned instead
     * 
     * @return an Action, the next in the scheduler from given index
     */
    @Override
    protected Action nextAction() {
        return null;
    }

}

2 个答案:

答案 0 :(得分:1)

您可以使用静态变量来跟踪索引。如果您执行相同的操作/否。对于所有调度程序实例的操作,则可以使用static变量来维护多个index类实例之间的FairScheduler变量的相同副本。

public class FairScheduler extends Scheduler {

    private static int index = 0;

    @Override
    /** removes a given action from the scheduler
     * @param action the action to remove
     */
    public void removeAction(Action action) {
        this.theActions.remove(action);
    }

    /** returns the nextAction in the scheduler, 
     * if the current action is the last element of the scheduler
     * the first action of the scheduler is returned instead
     * 
     * @return an Action, the next in the scheduler from given index
     */
    @Override
    protected Action nextAction() {
        if (!theActions.isEmpty()) {
            if (index >= theActions.size()){
                index = 0;
            }
            return theActions.get(index++);
        }
    }

}

答案 1 :(得分:0)

在我看来,您可以像FIFO或LIFO一样执行此操作

FIFO

@Override
public void removeAction(Action action) {
    if (!theActions.isEmpty() && action.isFinished()) {
        theActions.remove(action);
    }
}

@Override
public Action nextAction() {
    if (!theActions.isEmpty()) {
        return theActions.get(0);
    }
}

对于LIFO,您也可以这样做,但使用列表的最后一项。