在 while 循环中嵌套 for 循环 - Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3469847/
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
Nested for loop inside a while loop - Java
提问by Pritish
I was working on a Java web application, and had the following requirement with respect to looping in my HTML table.
我正在开发一个 Java Web 应用程序,并且对在我的 HTML 表中循环有以下要求。
I've a nested for loop inside a while loop(both execute the same # of times, for ex. say 3).
我在 while 循环中嵌套了一个 for 循环(两者都执行相同的次数,例如 3)。
My code looks something like this:
我的代码看起来像这样:
<table>
<thead>...</thead>
<tbody>
if (patcases != null && patcases.size() > 0) {
Iterator itr1 = patcases.iterator();
while (itr1.hasNext()) {
..some code here..
System.out.println("DA Email from webpage..."+da.getEmail());
int rCount = 0;
<tr>
for(int i=0;i<passedValues.length; i++){
...some code here..
</tr>
System.out.println("Printed row..." +rCount);
rCount ++;
} /*closing of for loop */
}/*closing of while loop */
}/* closing of if loop */
</tbody>
</table>
Now, with this type of looping structure, I get the following on my console:
现在,使用这种类型的循环结构,我在控制台上得到以下信息:
DA Email from [email protected]
Printed row...0
Printed row...1
Printed row...2
DA Email from [email protected]
Printed row...0
Printed row...1
Printed row...2
DA Email from [email protected]
Printed row...0
Printed row...1
Printed row...2
DA 来自网页的电子邮件[email protected] 已
打印的行...0 已
打印的行...1 已
打印的行...2
来自网页的 DA 电子邮件[email protected] 已
打印的行...0 已
打印的行...1 已
打印行...2
DA 来自网页的电子邮件[email protected] 已
打印行...0 已
打印行...1 已
打印行...2
But the type of output I wanted was, something as follows:
DA Email from [email protected]
Printed row...0
DA Email from [email protected]
Printed row...1
DA Email from [email protected]
Printed row...2
但我想要的输出类型如下:
来自网页的
DA 电子邮件[email protected]
打印的行...
0
DA 来自网页的电子邮件[email protected]
打印的行...1
DA来自网页的电子邮件[email protected]
打印行...2
How would I go about doing this?
Any help would be greatly appreciated.
我该怎么做呢?
任何帮助将不胜感激。
回答by polygenelubricants
It looks like you want parallel iteration.
看起来您想要并行迭代。
Simply do something like this:
简单地做这样的事情:
Iterator<?> iter1 = ...;
Iterator<?> iter2 = ...; // or: int index = 0;
while (iter1.hasNext() &&
iter2.hasNext()) { // or: index < MAX
Object item1 = iter1.next();
Object item2 = iter2.next(); // or: index++;
doSomething(item1, item2); // or: doSomething(item1, index);
}
// perhaps additional handling if one ran out before the other
Note that if at all possible, so you should use parameterized types instead of raw types (Effective Java 2nd Edition, Item 23: Don't use raw types in new code).
请注意,如果可能的话,您应该使用参数化类型而不是原始类型(Effective Java 2nd Edition,Item 23: Don't use raw types in new code)。
回答by Jon Skeet
It seems to me that you don't want a nested for loop at all. You just want a counter which gets incremented in the whileloop:
在我看来,您根本不需要嵌套的 for 循环。您只需要一个在while循环中递增的计数器:
if (patcases != null && patcases.size() > 0) {
Iterator itr1 = patcases.iterator();
int index = 0;
while (itr1.hasNext()) {
..some code here..
System.out.println("DA Email from webpage..."+da.getEmail());
if (index < passedValues.length) {
System.out.println("Printed row..." + index);
} else {
// Hmm, didn't expect this...
// (Throw exception or whatever)
}
index++;
}
if (index != passedValues.length) {
// Hmm, didn't expect this...
// (Throw exception or whatever)
}
}
回答by Joey Adams
"Nested for loops" doesn't mean "interlaced for loops". For example, saying:
“嵌套循环”并不意味着“交错循环”。例如说:
for (i = 0; i < 3; i++) {
print("i: " + i);
for (j = 0; j < 3; j++)
print("\tj: " + j);
}
prints the following:
打印以下内容:
i: 0
j: 0
j: 1
j: 2
i: 1
j: 0
j: 1
j: 2
i: 2
j: 0
j: 1
j: 2
What you seem to want is:
你似乎想要的是:
i: 0
j: 0
i: 1
j: 1
i: 2
j: 2
Instead of nesting forloops, you would just use a separate counter in the same loop:
for您只需在同一个循环中使用一个单独的计数器,而不是嵌套循环:
j = 0;
for (i = 0; i < 3; i++) {
print("i: " + i);
print("\tj: " + j);
j++;
}

