【问题标题】:Scope of variable declared in for loopfor循环中声明的变量范围
【发布时间】:2013-12-12 11:42:31
【问题描述】:

让我解释一下疑问。

我在 main 方法中有一个 int i = 9。现在我在同一个 main 方法中有 for 循环。看下面的例子

class Test {

  public static void main(String... args) throws Exception{   
    int i =39;
    for (int i = 0; i < 10; i++) {   // error:i already defined
    }
  }

}

对于上面的例子,它显示了 i 已经定义的编译时错误。从这个错误中我认为我在 for 条件中声明的范围也在循环之外。

现在看下面的例子

class Test {

  public static void main(String... args) throws Exception{   

    for (int i = 0; i < 10; i++) {
    }
    System.out.println(i); // error: cannot find symbol i
  }

}

在上面的例子中,它在 for 循环之外显示错误,找不到符号 i。

如果在 for 循环条件中未找到 i 声明。那么为什么显示 i 已在第一个示例中定义

【问题讨论】:

    标签: java scope


    【解决方案1】:

    根据Block的定义

    块是一组在平衡大括号之间的零个或多个语句,可以在任何允许使用单个语句的地方使用。

    所以

    {   //block started
    
    }    //block ended
    

    无论在块内声明的变量是什么,范围都限于该块。

    一个经验法则是变量的范围在{}

    public static void main(String... args) throws Exception{   
        int i =39;
        for (int i = 0; i < 10; i++) {   
         System.out.println(i); // which i ?? 
        }
      }
    

    在上述情况下,编译器混淆了它正在寻找的 i,因为 i 已经定义并且它也有一个可以在循环中访问的范围。

    main 方法范围内已经定义了i

    public static void main(String... args) throws Exception{   
    
        for (int i = 0; i < 10; i++) {
        }
        System.out.println(i); // the scope ended already with in {}
      }
    

    在上述情况下,i 范围已在 for {} 结束,并且外部不可用。

    【讨论】:

      【解决方案2】:

      在第一种情况下,i 具有方法范围。在声明之后,它在 main() 方法中随处可见。因此,当您尝试在 for 中声明另一个 i 时,它会给出已定义的错误。

        public static void main(String... args) throws Exception{   
          int i =39; // i will be visible throughout the main method
          for (int i = 0; i < 10; i++) {   // i already declared above, thus the error
          }
        }
      

      在第二种情况下,ifor 中声明,因此在其外部不可见。在for 循环中声明的变量i 只能通过{ for loop block } 访问,并且在右花括号之后超出范围。这就是为什么它在for循环之后的SOP语句中找不到符号i的原因。

        public static void main(String... args) throws Exception{   
          for (int i = 0; i < 10; i++) { // i is declared within the for
              // anywhere within this block, i is visible/in-scope
          } // after this closing braces of for, i has gone out of scope
          System.out.println(i); // thus the error: cannot find symbol i
        }
      

      【讨论】:

        【解决方案3】:

        第一个例子: i 范围是整个 main 方法

        第二个例子: i 范围是 for 循环

        干杯

        【讨论】:

          【解决方案4】:

          你可以想到

          for (int i = 0; i < 10; i++) {
          }
          

          相当于

          {
            int i;
            for (i = 0; i < 10; i++) {
            }
          }
          

          换句话说,声明的范围是for(...) 本身中的各种表达式,加上循环体,但没有超出循环体的结尾。

          在您的第一个示例中,您可以从 for 中删除 int 以使其成为赋值而不是声明:

          public static void main(String... args) throws Exception{   
            int i =39;
            for (i = 0; i < 10; i++) { // re-assigns the existing i variable
            }
            System.out.println(i); // prints 10
          }
          

          【讨论】:

            【解决方案5】:

            以下代码报错

            int i =39;
            for (int i = 0; i < 10; i++) {   // error:i already defined
            }
            

            下面不会报错

            for (int i = 0; i < 10; i++) {   
            }
            int i =39;
            

            【讨论】:

              【解决方案6】:

              在这个例子中

              class Test {
              
                public static void main(String... args) throws Exception{   
                  int i =39;
                  for (int i = 0; i < 10; i++) {   // error:i already defined
                  }
                }
              
              }
              

              您将 i 声明为 int 2 次,这在 java 中是不允许的,因为它们在同一范围内。

              现在在这个例子中

              class Test {
              
                public static void main(String... args) throws Exception{   
              
                  for (int i = 0; i < 10; i++) {
                  }
                  System.out.println(i); // error: cannot find symbol i
                }
              
              }
              

              i 的范围在 for 循环中,而您尝试在 for 循环之外打印 i

              这是工作代码

              class Test {
              
                public static void main(String... args) throws Exception{   
                  int i;
                      for ( i = 0; i < 10; i++) {
                      }
                      System.out.println(i); // error: cannot find symbol i
                    }
              
              }
              

              输出 10

              【讨论】:

                【解决方案7】:

                您所要做的就是在 for 循环之前添加另一个变量。然后,用你需要的 i 检查它。

                public static void main(String[] args) {
                    int j = 0;
                    for (int i = 0; i < 10; i++) {
                        j = i;
                    }
                    System.out.println(j);
                }
                

                【讨论】:

                  猜你喜欢
                  • 2015-11-29
                  • 2016-10-22
                  • 1970-01-01
                  • 2011-02-13
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多