Ale*_*der 24 java recursion loops for-loop
换句话说,我可以做些什么
for() {
for {
for {
}
}
}
Run Code Online (Sandbox Code Playgroud)
除了N次?换句话说,当调用创建循环的方法时,会给它一些参数N,然后该方法会创建N个嵌套在另一个中的N?
当然,我们的想法是应该采用"简单"或"通常"的方式.我已经有了一个非常复杂的想法.
joe*_*ely 33
jjnguy是对的; 递归可让您动态创建可变深度嵌套.但是,如果没有更多工作,您无法访问外层数据."嵌入式嵌套"案例:
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**
}
}
}
Run Code Online (Sandbox Code Playgroud)
保留变量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];
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
的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);
}
}
Run Code Online (Sandbox Code Playgroud)
和执行的(部分)输出:
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
Run Code Online (Sandbox Code Playgroud)
您可能想要解释您真正想做的事情.
如果外部for循环除了控制计数之外什么都不做,那么嵌套for循环只是一种更复杂的迭代方式,可以通过一个for循环来处理.
例如:
for (x = 0; x < 10; ++x) {
for (y = 0; y < 5; ++y) {
for (z = 0; z < 20; ++z) {
DoSomething();
}
}
}
Run Code Online (Sandbox Code Playgroud)
相当于:
for (x = 0; x < 10*5*20; ++x) {
DoSomething();
}
Run Code Online (Sandbox Code Playgroud)
前几天我正在考虑这件事.
一个可能不完美但与我的想法非常接近的例子是打印出一个目录树
public void printTree(directory) {
for(files in directory) {
print(file);
if(file is directory) {
printTree(file);
}
}
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式,你最终会将一堆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));
}
}
Run Code Online (Sandbox Code Playgroud)
导致
[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]
Run Code Online (Sandbox Code Playgroud)
它还支持除以外的条件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));
Run Code Online (Sandbox Code Playgroud)
导致:
[-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]
Run Code Online (Sandbox Code Playgroud)
显然,支持不同长度和不同类(所有盒装,数字基元)的循环.默认值(如果未指定)来自(0,...).by(1,...); 但必须指定一个到(...).
该NForTest文件应该演示几种不同的使用方法.
这个基本前提是每次转向简单地推进'指数'而不是使用递归.
| 归档时间: |
|
| 查看次数: |
17299 次 |
| 最近记录: |