以循环方式Java数组遍历 [英] java array traversal in circular manner
问题描述
我有一个有1 2 3 4 5值的数组。
阵列的= [1,2,3,4,5]
现在我想遍历它循环方式。
像我想打印2 3 4 5 1或3 4 5 1 2或5 1 2 3 4等。
任何算法在这?
编辑:我要打印的循环方式的所有组合。我不想状态开始在其初始阶段点。
INT开始= ...
的for(int i = 0; I<则为a.length;我++){
的System.out.println(一[(1 +开始)%则为a.length]);
}
我要指出,这可能不是前$ P $的pssing循环的最有效方式......在执行速度方面。然而,不同的是小的,并且最可能不相干的
一个更相关的问题是是否采用%
以这种方式提供了更多可读性code。我想这样做,但也许是因为我见过/用过这个特殊的成语。
I have an array which have 1 2 3 4 5 values.
array a = [ 1 , 2, 3, 4, 5]
Now i want to traverse it in circular manner. like i want to print 2 3 4 5 1 or 3 4 5 1 2 or 5 1 2 3 4 and so on. any algorithm on this?
Edit: I want to print all the combination in circular manner. i don't want to state starting point at its initial phase.
int start = ...
for (int i = 0; i < a.length; i++) {
System.out.println(a[(i + start) % a.length]);
}
I should note that this is probably not the most efficient way of expressing the loop ... in terms of execution speed. However, the difference is small, and most likely irrelevant.
A more relevant point is whether using %
in this way gives more readable code. I think it does, but maybe that's because I've seen / used this particular idiom before.
这篇关于以循环方式Java数组遍历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!