【发布时间】:2011-05-30 17:35:45
【问题描述】:
什么时候应该使用前减量,什么时候使用后减量?
对于下面的代码sn-p,我应该使用前减量还是后减量。
static private void function(int number)
{
charArr = new char[number];
int i = 0;
int tempCounter;
int j = 0;
while(charrArr!=someCharArr)
{
tempCounter = number - 1;
password[tempCounter] = element[i%element.Length];
i++;
//This is the loop the I want to use the decrementing in.
//Suppose I have a char array of size 5, the last element of index 5 is updated
//in the previous statement.
//About the upcoming indexes 4, 3, 2, 1 and ZERO.
//How should I implement it?
// --tempCounter or tempCounter-- ?
while (charArr[--tempCounter] == element[element.Length - 1])
{
}
}
}
【问题讨论】:
-
就您的代码而言,我猜它应该是
tempCounter = number;、password[tempCounter - 1]和charArr[--tempCounter],尽管while循环将在未初始化的数组上工作而tempCounter可以变成负数。