有没有办法在Java中进行n级嵌套循环?
题
换句话说,我可以做点什么吗
for() {
for {
for {
}
}
}
除了N次?换句话说,当调用创建循环的方法时,会给它一些参数N,然后该方法会创建N个嵌套在另一个中的N?
当然,我们的想法是应该有一个<!> quot; easy <!> quot;或<!>“通常的<!>”;这样做的方式。我已经有了一个非常复杂的想法。
解决方案
听起来您可能想要查看递归。
其他提示
jjnguy是对的;递归可让您动态创建可变深度嵌套。但是,如果没有更多工作,您无法访问外层数据。 <!> quot; in-line-nested <!> quot;情况下:
for (int i = lo; i < hi; ++i) {
for (int j = lo; j < hi; ++j) {
for (int k = lo; k < hi; ++k) {
// do something **using i, j, and k**
}
}
}
将变量i
,j
和k
保留在最内层正文的范围内。
这是一个快速破解的方法:
public class NestedFor {
public static interface IAction {
public void act(int[] indices);
}
private final int lo;
private final int hi;
private final IAction action;
public NestedFor(int lo, int hi, IAction action) {
this.lo = lo;
this.hi = hi;
this.action = action;
}
public void nFor (int depth) {
n_for (0, new int[0], depth);
}
private void n_for (int level, int[] indices, int maxLevel) {
if (level == maxLevel) {
action.act(indices);
} else {
int newLevel = level + 1;
int[] newIndices = new int[newLevel];
System.arraycopy(indices, 0, newIndices, 0, level);
newIndices[level] = lo;
while (newIndices[level] < hi) {
n_for(newLevel, newIndices, maxLevel);
++newIndices[level];
}
}
}
}
IAction
接口规定了受控操作的作用,该操作将索引数组作为其act
方法的参数。
在此示例中,NestedFor
的每个实例都由构造函数配置,其中包含迭代限制以及要由最内层执行的操作。 nFor
方法的参数指定嵌套的深度。
以下是一个示例用法:
public static void main(String[] args) {
for (int i = 0; i < 4; ++i) {
final int depth = i;
System.out.println("Depth " + depth);
IAction testAction = new IAction() {
public void act(int[] indices) {
System.out.print("Hello from level " + depth + ":");
for (int i : indices) { System.out.print(" " + i); }
System.out.println();
}
};
NestedFor nf = new NestedFor(0, 3, testAction);
nf.nFor(depth);
}
}
和执行的(部分)输出:
Depth 0
Hello from level 0:
Depth 1
Hello from level 1: 0
Hello from level 1: 1
Hello from level 1: 2
Depth 2
Hello from level 2: 0 0
Hello from level 2: 0 1
Hello from level 2: 0 2
Hello from level 2: 1 0
Hello from level 2: 1 1
Hello from level 2: 1 2
Hello from level 2: 2 0
Hello from level 2: 2 1
Hello from level 2: 2 2
Depth 3
Hello from level 3: 0 0 0
Hello from level 3: 0 0 1
Hello from level 3: 0 0 2
Hello from level 3: 0 1 0
...
Hello from level 3: 2 1 2
Hello from level 3: 2 2 0
Hello from level 3: 2 2 1
Hello from level 3: 2 2 2
你可能想解释一下你真正想做的事情。
如果外部for
循环除了控制计数之外什么都不做,那么嵌套的<=>循环只是一种更复杂的迭代方式,可以通过单个<=>循环来处理。
例如:
for (x = 0; x < 10; ++x) {
for (y = 0; y < 5; ++y) {
for (z = 0; z < 20; ++z) {
DoSomething();
}
}
}
相当于:
for (x = 0; x < 10*5*20; ++x) {
DoSomething();
}
前几天我正在考虑这件事。
一个可能不完美但与我认为被问到的非常接近的例子是打印出一个目录树
public void printTree(directory) {
for(files in directory) {
print(file);
if(file is directory) {
printTree(file);
}
}
}
这样你最终会将一堆for循环嵌套在彼此之内,而不必麻烦地弄清楚它们应该如何组合在一起。
2015编辑:与前一个咒语一样,我做了以下包来处理这个问题。 https://github.com/BeUndead/NFor
用法如下
public static void main(String... args) {
NFor<Integer> nfor = NFor.of(Integer.class)
.from(0, 0, 0)
.by(1, 1, 1)
.to(2, 2, 3);
for (Integer[] indices : nfor) {
System.out.println(java.util.Arrays.toString(indices));
}
}
导致
[0, 0, 0]
[0, 0, 1]
[0, 0, 2]
[0, 1, 0]
[0, 1, 1]
[0, 1, 2]
[1, 0, 0]
[1, 0, 1]
[1, 0, 2]
[1, 1, 0]
[1, 1, 1]
[1, 1, 2]
它还支持lessThan
以外的条件。其中的用法(带import static NFor.*;
):
NFor<Integer> nfor = NFor.of(Integer.class)
.from(-1, 3, 2)
.by(1, -2, -1)
.to(lessThanOrEqualTo(1), greaterThanOrEqualTo(-1), notEqualTo(0));
导致:
[-1, 3, 2]
[-1, 3, 1]
[-1, 1, 2]
[-1, 1, 1]
[-1, -1, 2]
[-1, -1, 1]
[0, 3, 2]
[0, 3, 1]
[0, 1, 2]
[0, 1, 1]
[0, -1, 2]
[0, -1, 1]
[1, 3, 2]
[1, 3, 1]
[1, 1, 2]
[1, 1, 1]
[1, -1, 2]
[1, -1, 1]
显然,支持不同长度和不同类(所有盒装,数字基元)的循环。默认值(如果未指定)来自(0,...)。by(1,...);但必须指定一个到(...)。
NForTest
文件应该演示几种不同的使用方法。
这个基本前提是每次转向简单地推进'指数',而不是使用递归。
问题需要更多规范。也许递归会对你有所帮助,但请记住,递归几乎总是迭代的替代方法,反之亦然。可能是2级嵌套循环足以满足您的需求。请告诉我们您要解决的问题。
嵌套循环背后的基本思想是乘法。
扩展Michael Burr的答案,如果外部for
循环除了控制计数之外什么都不做,那么嵌套n
循环X
计数只是迭代计数产品的一种更复杂的方式只有一个Y
循环。
现在,让我们将这个想法扩展到列表。如果您在嵌套循环中迭代三个列表,这只是一种使用单个循环迭代列表产品的更复杂方法。但是,您如何表达三个列表的产品?
首先,我们需要一种表达类型产品的方法。两种类型的产品P2<X, Y>
和P3<A, B, C>
可以表示为类似List<X>
的通用类型。这只是一个由两个值组成的值,一个是List<Y>
类型,另一个是List<Z>
类型。它看起来像这样:
public abstract class P2<A, B> {
public abstract A _p1();
public abstract B _p2();
}
对于三种类型的产品,我们只有List<P3<X, Y, Z>>
,有明显的第三种方法。然后,通过在产品类型上分发List仿函数来实现三个列表的产品。所以List
,doSomething
和<=>的产品就是<=>。然后,您可以使用单个循环遍历此列表。
Functional Java 库具有<=>类型,该类型支持使用一流函数和产品类型将列表相乘(P2,P3等也包含在库中)。
例如:
for (String x : xs) {
for (String y : ys) {
for (String z : zs) {
doSomething(x, y, z);
}
}
}
相当于:
for (P3<String, String, String> p : xs.map(P.p3()).apply(ys).apply(zs)) {
doSomething(p._1(), p._2(), p._3());
}
进一步使用Functional Java,您可以使<=>第一类,如下所示。假设<=>返回一个字符串:
public static final F<P3<String, String, String>, String> doSomething =
new F<P3<String, String, String>, String>() {
public String f(final P3<String, String, String> p) {
return doSomething(p._1(), p._2(), p._3());
}
};
然后你可以完全消除for循环,并收集<=>的所有应用程序的结果:
List<String> s = xs.map(P.p3()).apply(ys).apply(zs).map(doSomething);
如果你有一个通用的嵌套循环结构,如:
for(i0=0;i0<10;i0++)
for(i1=0;i1<10;i1++)
for(i2=0;i2<10;i2++)
....
for(id=0;id<10;id++)
printf("%d%d%d...%d\n",i0,i1,i2,...id);
其中i0,i1,i2,...,id
是循环变量,d
是嵌套循环的深度。
等效递归解决方案:
void nestedToRecursion(counters,level){
if(level == d)
computeOperation(counters,level);
else
{
for (counters[level]=0;counters[level]<10;counters[level]++)
nestedToRecursion(counters,level+1);
}
}
void computeOperation(counters,level){
for (i=0;i<level;i++)
printf("%d",counters[i]);
printf("\n");
}
计数器是一个大小为i0,i1,i2,...id
的数组,分别表示相应的变量int counters[d]
initial[d], ending[d]
。
nestedToRecursion(counters,0);
类似地,我们可以转换其他变量,如初始化递归或使用数组结束,即我们可以<=>。
我在Java 7中提出的最常用的方法是
// i[0] = 0..1 i[1]=0..3, i[2]=0..4
MultiForLoop.loop( new int[]{2,4,5}, new MultiForLoop.Callback() {
void act(int[] i) {
System.err.printf("%d %d %d\n", i[0], i[1], i[2] );
}
}
或者在Java 8中:
// i[0] = 0..1 i[1]=0..3, i[2]=0..4
MultiForLoop.loop( new int[]{2,4,5},
i -> { System.err.printf("%d %d %d\n", i[0], i[1], i[2]; }
);
支持此功能的实现是:
/**
* Uses recursion to perform for-like loop.
*
* Usage is
*
* MultiForLoop.loop( new int[]{2,4,5}, new MultiForLoop.Callback() {
* void act(int[] indices) {
* System.err.printf("%d %d %d\n", indices[0], indices[1], indices[2] );
* }
* }
*
* It only does 0 - (n-1) in each direction, no step or start
* options, though they could be added relatively trivially.
*/
public class MultiForLoop {
public static interface Callback {
void act(int[] indices);
}
static void loop(int[] ns, Callback cb) {
int[] cur = new int[ns.length];
loop(ns, cb, 0, cur);
}
private static void loop(int[] ns, Callback cb, int depth, int[] cur) {
if(depth==ns.length) {
cb.act(cur);
return;
}
for(int j = 0; j<ns[depth] ; ++j ) {
cur[depth]=j;
loop(ns,cb, depth+1, cur);
}
}
}
String fors(int n){
StringBuilder bldr = new StringBuilder();
for(int i = 0; i < n; i++){
for(int j = 0; j < i; j++){
bldr.append('\t');
}
bldr.append("for() {\n");
}
for(int i = n-1; i >= 0; i--){
for(int j = 0; j < i; j++){
bldr.append('\t');
}
bldr.append("}\n");
}
return bldr.toString();
}
创建一个漂亮的嵌套for循环骨架;-) 不完全严重,我知道递归解决方案会更优雅。
public void recursiveFor(Deque<Integer> indices, int[] ranges, int n) {
if (n != 0) {
for (int i = 0; i < ranges[n-1]; i++) {
indices.push(i);
recursiveFor(indices, ranges, n-1);
indices.pop();
}
}
else {
// inner most loop body, access to the index values thru indices
System.out.println(indices);
}
}
示例电话:
int[] ranges = {2, 2, 2};
recursiveFor(new ArrayDeque<Integer>(), ranges, ranges.length);
我第一次回答问题,但我觉得我需要分享这些信息 的 `
for (x = 0; x < base; ++x) {
for (y = 0; y < loop; ++y) {
DoSomething();
}
}
等同于
for (x = 0; x < base*loop; ++x){
DoSomething();
}
所以如果你想要n个巢,它可以用base
和loop
之间的区分来编写,所以看起来像这样简单:
char[] numbs = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
public void printer(int base, int loop){
for (int i = 0; i < pow(base, loop); i++){
int remain = i;
for (int j = loop-1; j >= 0; j--){
int digit = remain/int(pow(base, j));
print(numbs[digit]);
remain -= digit*pow(base, j);
}
println();
}
}
所以,如果您输入printer(10, 2);
,则会打印出来:
00
01
02
03
04
...
97
98
99
这对我很有用 - 我必须从一些替代品中选择,这些替代品存储在myAlternativePaths中,基本思路是我试图构建下一个选择,并且当有<!> quot; overflow <! > QUOT;在一个维度/组件中,您只需重新初始化该维度并将其添加到下一个维度。
public boolean isValidAlternativeSelection (int[] alternativesSelected) {
boolean allOK = true;
int nPaths= myAlternativePaths.size();
for (int i=0; i<nPaths; i++) {
allOK=allOK & (alternativesSelected[i]<myAlternativePaths.get(i).myAlternativeRoutes.size());
}
return allOK;
}
public boolean getNextValidAlternativeSelection (int[] alternativesSelected) {
boolean allOK = true;
int nPaths= myAlternativePaths.size();
alternativesSelected[0]=alternativesSelected[0]+1;
for (int i=0; i<nPaths; i++) {
if (alternativesSelected[i]>=myAlternativePaths.get(i).myAlternativeRoutes.size()) {
alternativesSelected[i]=0;
if(i<nPaths-1) {
alternativesSelected[i+1]=alternativesSelected[i+1]+1;
} else {
allOK = false;
}
}
// allOK=allOK & (alternativesSelected[i]<myAlternativePaths.get(i).myAlternativeRoutes.size());
}
return allOK;
}
为了简洁起见,我将代码放在这里:
void variDepth(int depth, int n, int i) {
cout<<"\n d = "<<depth<<" i = "<<i;
if(!--depth) return;
for(int i = 0;i<n;++i){
variDepth(depth,n,i);
}
}
void testVariDeapth()
{ variDeapth(3, 2,0);
}
输出
d = 3 i = 0
d = 2 i = 0
d = 1 i = 0
d = 1 i = 1
d = 2 i = 1
d = 1 i = 0
d = 1 i = 1