以下代码中的for循环是否多此一举?

import java.util.*;
public class test {
    @SuppressWarnings("deprecation")
    public static void main(String args[]) {
        Stack<Integer> stack = new Stack<Integer>();
        stack.push(new Integer(1));
        stack.push(new Integer(1));
        int k = 1;
        while(k <= 10) {
                for(int i = 1; i <= 2; i++) {  // 就是这个for循环,有没有它都不影响结果
                Integer F1 = stack.pop();
                int f1 = F1.intValue();
                Integer F2 = stack.pop();
                int f2 = F2.intValue();
                Integer temp = new Integer(f1 + f2);
                System.out.println("" + temp.toString());
                stack.push(F1);
                stack.push(temp);
                k++;
            }
        }
    }
}

这个是书上一个输出Fibonacci整数序列的示例代码,堆栈这一节的,利用堆栈避免递归。想了半天也没想明白这个for循环有何意义(有for循环:外层的while循环5次,for每次循环2次,5乘2=10;无for循环,直接循环10次),不都是一样的吗?

出处:java2实用教程(第五版)耿祥义、张跃平著 第451页

可能是作者原先想通过 for 循环两次,来操作栈中的数据,文中的 pop 操作进行了两次,如果用 for 循环的话,循环内的逻辑就不应该这样写了。

就这个程序本身来说

import java.util.*;
public class HelloWorld {
    public static void main(String []args) {
        Stack<Integer> stack = new Stack<Integer>();
        stack.push(new Integer(1));
        stack.push(new Integer(1));
        int k = 1;
        while(k <= 10) {
            //for(int i = 1; i <= 2; i++) {  // 就是这个for循环,有没有它都不影响结果
                Integer F1 = stack.pop();
                int f1 = F1.intValue();
                Integer F2 = stack.pop();
                int f2 = F2.intValue();
                Integer temp = new Integer(f1 + f2);
                System.out.println("" + temp.toString());
                stack.push(F1);
                stack.push(temp);
                k++;
            //}
        }
    }
}

删除后,结果一样
2
3
5
8
13
21
34
55
89
144

可能是作者忘记删除了,或者为了控制循环里的一些变量的作用域,或者为了为多个参数的递归的扩展而预留。