java 如何将此 for 循环转换为 while 循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36023297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I convert this for loop into a while loop?
提问by blackbox_devops
I am creating an array program. I am practicing how to convert for
loops to while
loops, but I cannot grasp the concept.
我正在创建一个数组程序。我正在练习如何将for
循环转换为while
循环,但我无法理解这个概念。
If I have the for
loop:
如果我有for
循环:
int [] list = new int [5];
for (int i = 0; i < 5; i++) {
list [i] = i + 2;
}
How would I make it a while
loop?
我将如何使它成为一个while
循环?
Here's my attempt
这是我的尝试
int [] list = new int [5];
int i = 0;
while (i<5) {
list [i] = i + 2;
i++;
}
System.out.print(list[i] + " ");
This is what I think should be done, but it comes up as an error in my computer.
这是我认为应该做的,但它在我的电脑中出现错误。
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at Arrays2.main(Arrays2.java:21)
线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 5 at Arrays2.main(Arrays2.java:21)
This is line 21
这是第 21 行
System.out.print(list[i] + " ");
回答by Andy Turner
The general structure of a basic for statement is:
基本for语句的一般结构是:
for ( ForInit ; Expression ; ForUpdate ) Statement
ForInit
is the initializer. It is run first to set up variables etc.Expression
is a boolean condition to check to see ifStatement
should be runStatement
is the block of code to be run ifExpression
is trueForUpdate
is run after theStatement
to e.g. update variables as necessary
ForInit
是初始化程序。它首先运行以设置变量等。Expression
是一个布尔条件,用于检查是否Statement
应该运行Statement
如果Expression
为真,则是要运行的代码块ForUpdate
Statement
根据需要在to 例如更新变量之后运行
After ForUpdate
has been run, Expression
is evaluated again. If it is still true, Statement
is executed again, then ForUpdate
; repeat this until Expression
is false.
ForUpdate
运行完毕后,Expression
再进行评估。如果仍然为真,Statement
则再次执行,则ForUpdate
;重复这个直到Expression
是假的。
You can restructure this as a while loop as follows:
您可以将其重组为一个 while 循环,如下所示:
ForInit;
while (Expression) {
Statement;
ForUpdate;
}
In order to apply this pattern to a "real" for loop, just substitute your blocks as described above.
为了将此模式应用于“真正的”for 循环,只需按照上述方法替换您的块。
For your example above:
对于上面的示例:
ForInit
=>int i = 0
Expression
=>i < 5
ForUpdate
=>i++
Statement
=>list [i] = i + 2;
ForInit
=>int i = 0
Expression
=>i < 5
ForUpdate
=>i++
Statement
=>list [i] = i + 2;
Putting it together:
把它放在一起:
int i = 0;
while (i < 5) {
list[i] = i + 2;
i++;
}
回答by spfrommer
int [] list = new int [5];
int i = 0; // initialization
while (i < 5) { // condition
list [i] = i + 2;
i++; // afterthought
}
The for loop posted is pretty much a shorthand for the above. The first part of a for loop is called the initialization, equivalent to the int i = 0;
in the above code. The next is called the condition, which, if true, will cause the loop to be run again; this is the i < 5
part. Finally there is the afterthought, which changes the iterator i
(usually an increment of one for iterating over an array). The for loop simply condenses these three parts into one line, as such:
发布的 for 循环几乎是上述内容的简写。for 循环的第一部分称为初始化,相当于int i = 0;
上面代码中的 。下一个称为条件,如果为真,将导致循环再次运行;这是i < 5
部分。最后是事后的想法,它改变了迭代器i
(通常在迭代数组时增加 1)。for 循环简单地将这三个部分压缩为一行,如下所示:
for (initialization; condition; afterthought) {
}
回答by kajacx
Your while
loop is perfetly fine, but after it ends, the value of i
is 5
. Since java arrays are always indexed from zero, list[5]
doesn't exist and accessing it throws exception.
你的while
循环是perfetly很好,但它结束后,价值i
为5
。由于 java 数组总是从零开始索引,因此list[5]
不存在并且访问它会引发异常。
That's what you see when printing System.out.print(list[i] + " ");
Just print any other element, list[1]
, list[4]
or list[0]
for example.
这就是你看到打印时System.out.print(list[i] + " ");
只需打印任何其他元素,list[1]
,list[4]
或list[0]
例如。