简体   繁体   English

在Java中声明变量外部Foreach循环

[英]Declaring Variable Outside Foreach Loop in Java

Can someone please enlighten me on the following matter: 有人可以请赐教我以下事项:

public class Loopy {
    public static void main(String[] args)
    {
        int[] myArray = {7, 6, 5, 4, 3, 2, 1};

        int counterOne; 
        for (counterOne = 0; counterOne < 5; counterOne++) {
            System.out.println(counterOne + " ");
        }
        System.out.println(counterOne + " ");

        int counterTwo = 0; 
        for (counterTwo : myArray) {
            System.out.println(counterTwo + " ");
        }

    }

}

In the for-loop, we declare counterOne outside the loop and use it inside the loop. 在for循环中,我们在循环外声明counterOne并在循环内使用它。 This is correct, so long as we don't use counterOne after the loop is completed. 这是正确的,只要我们在循环完成后不使用counterOne

In the foreach-loop, we also declare counterTwo outside the loop and then use it only inside the loop. 在foreach循环中,我们还在循环外声明counterTwo ,然后在循环内部使用它。 However, an error is thrown in this case: 但是,在这种情况下会抛出错误:

"Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: class counterTwo location: class package1.Loopy" “线程中的异常”主“java.lang.RuntimeException:无法编译的源代码 - 找不到符号符号:class counterTwo location:class package1.Loopy”

Can you help me understand why? 你能帮我理解为什么吗?

The only difference between the two, is that counterOne is initialized to zero, and then is assigned values incrementally (smaller than 5). 两者之间的唯一区别是, counterOne初始化为零,然后逐步分配值(小于5)。

In the foreach loop, counterTwo is assigned one by one, each array item. 在foreach循环中, counterTwocounterTwo分配,每个数组项。

The program works if we make this adjustment in the second for loop: for(int counterTwo : myArray) , while the first for works in both cases: 如果我们在第二个for循环中进行调整,该程序可以工作: for(int counterTwo : myArray) ,而第一个适用于两种情况:

  1. the existing one 现有的
  2. for (counterOne = 0; counterOne < 5; counterOne++)

From this section of the Java Language Specification about enhanced for -loops: Java语言规范的这一部分开始,关于增强的for -loops:

The enhanced for statement has the form: 增强的for语句具有以下形式:

EnhancedForStatement: EnhancedForStatement:

for ( {VariableModifier} UnannType VariableDeclaratorId : Expression ) Statement

EnhancedForStatementNoShortIf: EnhancedForStatementNoShortIf:

for ( {VariableModifier} UnannType VariableDeclaratorId : Expression ) StatementNoShortIf

Note that the type declaration UnannType must be present in the for loop. 请注意,类型声明UnannType必须存在于for循环中。 Therefore, you should write the loop as follows: 因此,您应该按如下方式编写循环:

for (int z : x) {

Well, making it simple, the second one is a "special" for, it's actually a "for each". 好吧,简单,第二个是“特殊”,它实际上是“为每个人”。 It always needs the variable declaration inside. 它总是需要内部的变量声明。 Instead of explaining it badly, here you are the link to an older question about this, check it out: Why is declaration of the variable required inside a foreach loop 而不是解释它,这里你是一个关于这个的旧问题的链接,检查出来: 为什么在foreach循环内需要声明变量

This is just the syntax of a for each loop. 这只是每个循环的语法。 You cannot use a variable defined within the for each loop outside of the loop itself. 您不能在循环本身之外的每个循环中使用定义的变量。 It is just how the language is defined. 这就是语言的定义方式。

https://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html https://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM