以循环方式Java数组遍历 [英] java array traversal in circular manner

查看:154
本文介绍了以循环方式Java数组遍历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆