gpt4 book ai didi

java - 不带参数访问列表中的下一个元素

转载 作者:行者123 更新时间:2023-12-02 10:39:28 25 4
gpt4 key购买 nike

作为练习,我获得了一些功能的核心,我必须实现那些缺失的功能。我们正在研究调度程序和操作:

操作类:

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;
}

}

调度程序类:

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) 访问下一个元素 对于这样一个小任务来说,创建一个迭代器似乎需要很多时间

我正在 fairScheduler 类中实现 nextAction() :

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;
}

}

最佳答案

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

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++);
}
}

}

关于java - 不带参数访问列表中的下一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53032856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com