You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何利用final变量actionLimit为Decrementing Carousel创建调用次数限制?

实现带调用次数限制的DecrementingCarousel组件

核心接口定义

首先定义CarouselRun接口,规范容器运行时的核心方法:

public interface CarouselRun {
    // 递减下一个可用元素并返回当前值,无可用元素时返回-1
    int next();
    // 判断是否无元素可递减或已达到调用次数限制
    boolean isFinished();
}

抽象基类封装状态逻辑

CarouselBase作为抽象基类,统一管理容器的状态(累积/运行)和元素存储:

public abstract class CarouselBase {
    protected final int[] elements;
    protected int size;
    protected boolean isRunning;

    protected CarouselBase(int capacity) {
        if (capacity <= 0) {
            throw new IllegalArgumentException("容器容量必须为正整数");
        }
        elements = new int[capacity];
        size = 0;
        isRunning = false;
    }

    // 仅在累积状态下可添加正整数元素,添加成功返回true
    public boolean addElement(int element) {
        if (isRunning || element <= 0 || size >= elements.length) {
            return false;
        }
        elements[size++] = element;
        return true;
    }

    // 切换至运行状态并返回对应的CarouselRun实例,重复调用返回null
    public abstract CarouselRun run();
}

基础递减容器实现

DecrementingCarousel实现基础的递减逻辑,返回标准的CarouselRun实例:

public class DecrementingCarousel extends CarouselBase {

    public DecrementingCarousel(int capacity) {
        super(capacity);
    }

    @Override
    public CarouselRun run() {
        if (isRunning) {
            return null;
        }
        isRunning = true;
        return new StandardCarouselRun();
    }

    private class StandardCarouselRun implements CarouselRun {
        private int currentIndex = 0;

        @Override
        public int next() {
            if (isFinished()) {
                return -1;
            }
            // 跳过已减至0的元素,循环遍历数组
            while (elements[currentIndex] == 0) {
                currentIndex = (currentIndex + 1) % size;
            }
            int currentValue = elements[currentIndex];
            elements[currentIndex]--;
            currentIndex = (currentIndex + 1) % size;
            return currentValue;
        }

        @Override
        public boolean isFinished() {
            for (int i = 0; i < size; i++) {
                if (elements[i] > 0) {
                    return false;
                }
            }
            return true;
        }
    }
}

带调用次数限制的子类实现

DecrementingCarouselWithLimitedRun继承基础容器,通过构造函数传入actionLimit限制next()的调用次数:

public class DecrementingCarouselWithLimitedRun extends DecrementingCarousel {
    private final int actionLimit;

    public DecrementingCarouselWithLimitedRun(int capacity, int actionLimit) {
        super(capacity);
        if (actionLimit <= 0) {
            throw new IllegalArgumentException("调用次数限制必须为正整数");
        }
        this.actionLimit = actionLimit;
    }

    @Override
    public CarouselRun run() {
        if (isRunning) {
            return null;
        }
        isRunning = true;
        return new LimitedCarouselRun();
    }

    private class LimitedCarouselRun implements CarouselRun {
        private int currentIndex = 0;
        private int actionCount = 0;

        @Override
        public int next() {
            if (isFinished()) {
                return -1;
            }
            // 跳过已减至0的元素
            while (elements[currentIndex] == 0) {
                currentIndex = (currentIndex + 1) % size;
            }
            int currentValue = elements[currentIndex];
            elements[currentIndex]--;
            currentIndex = (currentIndex + 1) % size;
            actionCount++;
            return currentValue;
        }

        @Override
        public boolean isFinished() {
            // 两个终止条件:已达到调用次数限制,或所有元素已减至0
            return actionCount >= actionLimit || super.isFinished();
        }
    }
}

关键逻辑说明

  • 状态切换:调用run()后容器进入运行状态,禁止后续添加元素,重复调用run()返回null。
  • 元素遍历:next()方法通过循环指针遍历数组,自动跳过已减至0的元素,保证每次返回有效递减值。
  • 次数限制:LimitedCarouselRun维护调用计数器actionCount,每次next()调用后递增;isFinished()优先检查是否达到actionLimit,满足则直接判定为完成。

内容的提问来源于stack exchange,提问作者Durynda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 22:45:25