Java 可自定义嵌套 for 循环的数量

标签 java for-loop

我正在尝试制作可自定义数量的嵌套 for 循环,就像这样

int size=4;
for ...
  for ...
    for ...
      for ...

并且在改变大小时,创建一个新的嵌套循环

int size=6;
for ...
  for ...
    for ...
      for ...
        for ...
          for ...

我一直在尝试和测试,最后我得到了这个:

    // The amount of nested loops
    int size=3;

    // Variables
    String output="";
    String code="";
    String home=new File("").getAbsolutePath();

    // Generating code

    // Opening
    code+="public class Temp { \n";
    code+="    public static void main(String[] args) { \n";

    String depth="        ";

    // Making the variables
    code+=depth+"int[] data = new int["+size+"];\n";
    code+=depth+"String output = \"\";\n";

    // Making the for loops
    for(int i=0; i<size; i++) {
        // Adding formatting
        for(int x=i; x>0; x--)
            code+="    ";

        // Creating for-loop
        code+=depth+"for (data["+i+"]=0; data["+i+"]<10; data["+i+"]++) {\n";
    }

    // Adding formatting
    for(int x=0; x<size; x++)
        code+="    ";

    // Making the output (data[0]+""+data[1]+""+data[2]+"" etc)
    code+=depth+"output+=";
    for(int i=0; i<size; i++) {
        code+="data["+i+"]+\"\"";
        if(i<size-1)
            code+="+";
    }
    code+=";\n";

    // Adding formatting
    for(int x=0; x<size; x++)
        code+="    ";

    // Adding a newline after the output
    code+=depth+"output+=\"\\n\";\n";

    // Adding formatting and closing for-loops
    for(int i=0; i<size; i++) {
        for(int x=i; x<size-1; x++)
            code+="    ";
        code+=depth+"}\n";
    }

    // Outputting the variable output
    code+=depth+"System.out.println(output);\n";
    code+="    }\n";
    code+="}\n";

    // Outputting the code (for debugging purposes)
    System.out.println(code);

    // Compiling, running and getting output
    try {
        // Making a file called Temp.java
        FileOutputStream fos=new FileOutputStream("Temp.java");
        byte[] buf=code.getBytes();
        fos.write(buf);
        fos.close();

        System.out.println("===== ===== ===== ===== ===== =====");
        System.out.println("\n=====        Compiling        =====\n");

        // Executing
        Process p=Runtime.getRuntime().exec("javac -d "+home+" Temp.java");

        // Getting output and error
        InputStream is=p.getInputStream();
        InputStream es=p.getErrorStream();

        buf=new byte[8192];
        byte[] ebuf=new byte[8192];

        is.read(buf);
        es.read(ebuf);

        System.out.println(new String(buf).trim());
        System.err.println(new String(ebuf).trim());

        System.out.println("\n=====         Running         =====");

        // Executing
        p=Runtime.getRuntime().exec("java Temp");

        // Getting output and error
        is=p.getInputStream();
        es=p.getErrorStream();

         buf=new byte[8192];
        ebuf=new byte[8192];
        is.read(buf);
        es.read(ebuf);

        // Make output the value of the external 'output'
        output=new String(buf).trim();
        System.err.println(new String(ebuf).trim());

        System.out.println("\n=====   Removing temp files   =====");

        // Executing
        p=Runtime.getRuntime().exec("rm -f Temp.java Temp.class");

        // Getting output and error
        is=p.getInputStream();
        es=p.getErrorStream();

        buf=new byte[8192];
        ebuf=new byte[8192];
        is.read(buf);
        es.read(ebuf);

        System.out.println(new String(buf).trim());
        System.err.println(new String(ebuf).trim());

        System.out.println("\n===== ===== ===== ===== ===== =====");

    } catch(Exception e) {
        e.printStackTrace();
    }

    // Outputting the output
    System.out.println("output\n"+output);

它可以正常工作,但我讨厌创建、编译和运行外部 Java 文件的方式。有没有更好的方法可以在不使用外部文件的情况下做同样的事情?

最佳答案

要么递归是答案(如上所述),要么在数组中管理您的“i”变量:

public void nestedLoop(int size, int loopSize) {
    int[] i = new int[size];
    while (i[size-1] < loopSize) {
       doSomethingWith(i);
       increment(i, loopSize);
    }
}

public void increment(int[] i, int maxSize) {
   int idx = 0;
   while (idx < i.length) {
      if (++i[idx] < maxSize) {
         return;
      }
      i[idx++] = 0;
   }
}

关于Java 可自定义嵌套 for 循环的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27472707/

相关文章:

python - 不进入python 2.7.2中的for循环

python - 确定条件是否适用于列表的所有成员

java - 真的有必要调用 java.lang.Object 构造函数吗?

java - "low risk"JDO 或 JPA 之间的选择是什么?

Java 与静态 Map 中的条目同步

javascript - Ajax 循环调用

java - 将参数传递给 JSF 函数

java - 如何避免根附加器附加到子记录器

java - 嵌套循环以查找计数

php - 更新一列中的完整数组值