本文介绍了for循环变量声明中允许的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我做了一些搜索,找不到在for循环初始化语句中使用的有效类型列表。 循环变量声明中是否有可以在中使用的固定列表类型?例如,考虑下面的代码: for(int i = 0; i for(String str =a; str.length()< 10; str + =a)// ... 第一部作品,但我不认为第二部作品。有没有一个在for循环初始化中允许的所有类型的列表?解决方案查看Java语言规范对于 为语句。你可以在中为循环声明和初始化任何类型的变量,甚至可以声明多个变量,只要它们是相同的类型。语法中的相关产品是: $ p $ $ $ $ $ $ $ $ Basic $语句 for(ForInitopt; Expressionopt; ForUpdateopt) ForInit: StatementExpressionList LocalVariableDeclaration $ b LocalVariableDeclaration: VariableModifiersopt类型VariableDeclarators VariableDeclarators: VariableDeclarator VariableDeclarators,VariableDeclarator 这意味着您可以执行以下任何操作,例如 for(int i = 0; ...; ...)//变量声明与初始值 for(int i = 0,j = 1; ...; ...)// (final Iterator< T> it = ...; ...; ...)//最终变量 这里的第一个例子显示你根本不需要任何变量,并且作为在评论中指出,你不必有 ForUpdate 。唯一的约束是你必须在中间有一个表达式,它需要是一个布尔值表达式。另外, ForInit 也可以是 StatementExpressionList ,这意味着不用声明和初始化变量,也可以执行一些语句。例如,你可以做到这一点(但这不是一个特别有用的例子): for (System.out.println(begin loop; ...; ...) 这可能很有用,我想,在模拟 do / while 循环(如果你想这样做),如果body是一个简单的函数调用: for(method(); condition; method()); I've done some searches and couldn't find a list of valid types to use in for loop initialization statements. Is there a fixed list of types that can be used in for loop variable declarations? For instance, consider the following code:for (int i = 0; i < 5; i++) // ...for (String str = "a"; str.length() < 10; str+="a") // ...The first works, but I don't think the second will. Is there a list of all the types which are permitted in a for loop initialization? 解决方案 Have a look at the Java language specification for the for statement. You can declare and initialize any type of variable in a for loop, and can even declare multiple variables, so long as they're all the same type. The relevant productions in the grammar are:BasicForStatement: for ( ForInitopt ; Expressionopt ; ForUpdateopt ) StatementForInit: StatementExpressionList LocalVariableDeclarationLocalVariableDeclaration: VariableModifiersopt Type VariableDeclaratorsVariableDeclarators: VariableDeclarator VariableDeclarators , VariableDeclaratorThis means that you can do any of the following, e.g., for ( ; … ; … ) // no variable declaration at allfor ( int i; … ; … ) // variable declaration with no initial valuefor ( int i=0; … ; … ) // variable declaration with initial valuefor ( int i=0, j=1; … ; … ) // multiple variablesfor ( final Iterator<T> it = …; … ; … ) // final variableThe first example there shows that you don't need any variables at all, and as pointed out in the comments, you don't have to have a ForUpdate either. The only constraint is that you must have an expression in the middle, and it needs to be a boolean valued expression.As an aside, the ForInit can also be a StatementExpressionList, which means that instead of declaring and initializing variables, you can also just execute some statements. E.g, you could do this (but this isn't a particularly useful example):for ( System.out.println( "beginning loop" ; … ; … )This could be useful, I suppose, in simulating a do/while loop (if you wanted to do that), if the body is a simple function call:for ( method() ; condition ; method() ); 这篇关于for循环变量声明中允许的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-19 11:05