1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
//list = {"Bill","Bob","Jane","Tyler","Joe"}
//say that position=3 (so before "Jane") and newName = "Anne"
//so the wanted result is {"Bill","Bob","Anne","Jane","Tyler","Joe"}
//for (int i = position - 1; i < 10 - position; i++)
for (int i = 3 - 1; i < 10 - 3; i++) //traverse the array from index 2 to 7 (¿?)
{
if (i == (position - 1))
{
//this will only execute on the first iteration (perhaps should be outside the loop)
//i=2
temp = list[i];
list[i] = newName;
//so now we have temp = "Jane"
//list = {"Bill","Bob","Anne","Tyler","Joe"}
}
else if (i > (position - 1))
{
//let's check the second iteration, i=3, temp="Jane"
list[i] = temp;
//list = {"Bill","Bob","Anne","Jane","Joe"}
//there you lost "Tyler", I told you to not overlap the cards
temp = list[i + 1];
//here temp = "Joe"
//in the next iteration you'll do list[4] = temp, so "Joe"="Joe"
//so you won't change the rest of the array
}
}
|