gpt4 book ai didi

java:while循环-在进入花括号之间的语句之前使用分号的语句?

转载 作者:行者123 更新时间:2023-12-01 07:35:27 35 4
gpt4 key购买 nike

我在 stackoverflow 上查看另一个页面,发现了循环排序的工作实现,但我不明白带有分号的语句如何存在于 while 循环中的大括号之前。我认为 while 循环一旦找到带分号的语句就应该完全终止并且不采取进一步的操作,那么大括号内的代码是如何执行的呢?乍一看,我会将其解释为“var”随着 while 循环的每次迭代而递增 - 但我知道情况并非如此,因为从该位置删除它并将“var++”放入花括号内会导致无限环形。

“var”到底在什么条件下递增?解释或解释类似语法的链接:

while (checkSomeBool) var++;
{
//other stuff happening in here
}

将不胜感激。谢谢。以下是取自 CycleSort 的代码

public static final <T extends Comparable<T>> int cycleSort(final T[] array) {
int writes = 0;

// Loop through the array to find cycles to rotate.
for (int cycleStart = 0; cycleStart < array.length - 1; cycleStart++) {
T item = array[cycleStart];

// Find where to put the item.
int pos = cycleStart;
for (int i = cycleStart + 1; i < array.length; i++)
if (array[i].compareTo(item) < 0) pos++;

// If the item is already there, this is not a cycle.
if (pos == cycleStart) continue;

// Otherwise, put the item there or right after any duplicates.
<while (item.equals(array[pos])) pos++;
{
final T temp = array[pos];
array[pos] = item;
item = temp;
}
writes++;

// Rotate the rest of the cycle.
while (pos != cycleStart) {
// Find where to put the item.
pos = cycleStart;
for (int i = cycleStart + 1; i < array.length; i++)
if (array[i].compareTo(item) < 0) pos++;

// Put the item there or right after any duplicates.
while (item.equals(array[pos])) pos++;
{
final T temp = array[pos];
array[pos] = item;
item = temp;
}
writes++;
}
}
return writes;

}

最佳答案

while循环以var++结束

while (checkSomeBool) var++; // while ends here

之后的代码根本不是 while 循环的一部分。

{
//other stuff happening in here - not part of the while loop
}

关于java:while循环-在进入花括号之间的语句之前使用分号的语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12658242/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com