我在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;
}