转换循环总是像这样工作吗?或者有一些情况不是?当我在考试中解决这样的问题时,最快的方法是什么?转换循环(Java初学者问题)
For循环:
for(xx;yy;zz)
{
aa
}
While循环:
xx
while(yy)
{
aa
zz
}
做,当:
xx
do
{
aa
zz
}
while(yy)
转换循环总是像这样工作吗?或者有一些情况不是?当我在考试中解决这样的问题时,最快的方法是什么?转换循环(Java初学者问题)
For循环:
for(xx;yy;zz)
{
aa
}
While循环:
xx
while(yy)
{
aa
zz
}
做,当:
xx
do
{
aa
zz
}
while(yy)
你有几个失误。特别是do-while
循环是不正确的,因为它总是执行至少一次迭代,而对于for
和while
循环而言则不是这种情况。
for(x(); y(); z())
{
a();
}
x();
while(y())
{
a();
z();
}
x();
if(y())
{
do
{
a();
z();
} while(y())
}
你能告诉我一个将for-loop转换为do-while的例子吗?你是对的,我在做Do-while时总会犯错误。非常感激。 – 2011-04-16 22:29:06
@Mohammad:在这个答案中有一个do-while-loop的例子,它看起来等同于顶部的for-loop。 – 2011-04-16 23:15:49
我不认为你的理解是正确的。一段时间保证至少执行一次代码。 – 2011-04-17 00:02:54
一个for
声明和while
陈述或do-while
语句之间的唯一区别是,for
还指定初始化和迭代的增量。
例如,让我们的Iterator
这在Java可以被用于通过一个载体或类似的东西(如链接列表)迭代:
//we already have a variable v whom type implements List interface;
for (Iterator vectorIterator = v.iterator(); vectorIterator.hasNext();) {
//I left blank after the last ";" because there should come
// the increment statement, and I need to store the .next() return value.
int current = vectorIterator.next();
}
这里是while
版本:
Iterator vectorIterator = v.iterator();
while (vectorIterator.hasNext()) {
int current = vectorIterator.next();
}
do-while
与while
版本非常相似。
我给你提供了这个更复杂的例子,告诉你任何你用while
做的事情,无论多么复杂,你也可以用for
做。
EDIT:所述do-while
语句存在作为替代while
,条件是所述循环条件是在与迭代的检查的唯一区别。所以,你可以做一个相当于上面的例子中使用do-while
这样的:
Iterator vectorIterator = v.iterator();
if (vectorIterator.hasNext()) {
do {
int current = vectorIterator.next();
} while (vectorIterator.hasNext());
}
但在这样做没有任何目的。您应该使用每个工具来达到自己的目的。
其他成员已经涵盖了执行语义。但我想添加变量范围语义。
在Java中,你可以这样做:
for (Iterator<String> i = localList.iterator(); i.hasNext();) {
String element = i.next();
if (...) {
i.remove();
}
}
for (Iterator<String> i = importList.iterator(); i.hasNext();) {
String element = i.next();
if (...) {
i.remove();
}
}
其中变量 'i' 是使用了两次。
为了翻译,要同时用正确的作用域循环,你需要使用额外的代码块:
{
Iterator<String> i = localList.iterator();
while (i.hasNext()) {
String element = i.next();
if (...) {
i.remove();
}
}
}
{
Iterator<String> i = importList.iterator();
while (i.hasNext()) {
String element = i.next();
if (...) {
i.remove();
}
}
}
注意额外{ ... }
如何允许您使用相同的变量名。
尽管Java在C++等语言中不那么重要,但范围界定除了能够使用相同的变量名之外,还具有重要的影响。对象破坏,IIA(实例化是获取)等受附加块的影响。
// This class demonstrates the transliteration
// between while, for, and for-each loop syntax
public class LoopTest {
// This method definition showcases do-while-loop syntax
public void doWhileLoop(Person[] people) {
int i=0;
do {
Person p = people[i];
System.out.println(p.getName());
i++;
}while(i<people.length);
}
// This method definition showcases while-loop syntax
public void whileLoop(Person[] people) {
int i=0;
while(i<people.length) {
Person p = people[i];
System.out.println(p.getName());
i++;
}
}
// This method definition showcases for-loop syntax
public void forLoop(Person[] people) {
for(int i=0; i<people.length; i++) {
Person p = people[i];
System.out.println(p.getName());
}
}
// This method definition showcases for-each-loop syntax
public void forEachLoop(Person[] people) {
for(Person p : people) {
System.out.println(p.getName());
}
}
}
你的意思是'aa'而不是'yy'在while循环体内吗? – jonsca 2011-04-16 21:47:45