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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:52:46  来源:igfitidea点击:

How do I convert this for loop into a while loop?

javaarraysfor-loopwhile-loop

提问by blackbox_devops

I am creating an array program. I am practicing how to convert forloops to whileloops, but I cannot grasp the concept.

我正在创建一个数组程序。我正在练习如何将for循环转换为while循环,但我无法理解这个概念。

If I have the forloop:

如果我有for循环:

int [] list = new int [5];

for (int i = 0; i < 5; i++) {
    list [i] = i + 2;
}

How would I make it a whileloop?

我将如何使它成为一个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
  • ForInitis the initializer. It is run first to set up variables etc.
  • Expressionis a boolean condition to check to see if Statementshould be run
  • Statementis the block of code to be run if Expressionis true
  • ForUpdateis run after the Statementto e.g. update variables as necessary
  • ForInit是初始化程序。它首先运行以设置变量等。
  • Expression是一个布尔条件,用于检查是否Statement应该运行
  • Statement如果Expression为真,则是要运行的代码块
  • ForUpdateStatement根据需要在to 例如更新变量之后运行

After ForUpdatehas been run, Expressionis evaluated again. If it is still true, Statementis executed again, then ForUpdate; repeat this until Expressionis 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 < 5part. 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 whileloop is perfetly fine, but after it ends, the value of iis 5. Since java arrays are always indexed from zero, list[5]doesn't exist and accessing it throws exception.

你的while循环是perfetly很好,但它结束后,价值i5。由于 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]例如。