首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在数字列表中循环

在数字列表中循环
EN

Stack Overflow用户
提问于 2014-10-16 13:37:29
回答 2查看 98关注 0票数 0

我如何在java中循环数字,以便

1=1、2=2、3=3、4=1、5=2、6=3、7=1等。

从而在有限的可能性集合中包含无限的数字集合?

EN

回答 2

Stack Overflow用户

发布于 2014-10-16 13:45:48

代码语言:javascript
复制
public int modulo(int a, int b) {
    int r = a % b;
    return r==0?b:r;
}

assert(modulo(1,3) == 1)
assert(modulo(2,3) == 2)
assert(modulo(3,3) == 3)
assert(modulo(4,3) == 1)
assert(modulo(5,3) == 2)
票数 0
EN

Stack Overflow用户

发布于 2014-10-16 14:33:51

代码语言:javascript
复制
import static com.google.common.collect.Iterables.cycle;
import static com.google.common.collect.Iterables.limit;
import static com.google.common.collect.Lists.newArrayList;

System.out.println(newArrayList(limit(cycle(1, 2, 3), 7)));
// [1, 2, 3, 1, 2, 3, 1]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26396942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档