0

I know this loop will print {5, 1, 2, 3, 4} which is intended. The question here is how does the line below work?

values[i + 1] = values[i];

From what I understand, during the first loop, i = 3. So it will be values[3+1] = values[3] which would mean on the 4th index there will be the element of 3. Which is clearly wrong as that is not how the code works as the output is different.

int[] values = new int[]
        {1, 2, 3, 4, 5};
int temp = values[values.length - 1];
for (int i = values.length - 2; i >= 0; i--)
{
    values[i + 1] = values[i];
}
values[0] = temp;
System.out.println(Arrays.toString(values));

The code shifts the elements of the array to the right and shifts the rightmost element to the leftmost position.

Please explain to me because I am confused. Thank you.

7
  • 1
    The best way to understand this is to step through it statement by statement in the debugger watching what the array has in it as you go. Commented Oct 19, 2016 at 9:46
  • I have but i still don't understand. Commented Oct 19, 2016 at 9:47
  • this is nothing fancy, if you want to know more, just print the array content in loop after every iteration. Commented Oct 19, 2016 at 9:48
  • Visualised Example Commented Oct 19, 2016 at 9:49
  • Checkout this link stackoverflow.com/questions/7970857/… Commented Oct 19, 2016 at 9:50

3 Answers 3

1

They key is to watch how values changes across the course of the loop. Since this isn't language-specific, let's look at it in JavaScript so we can run it here on-site (I've also changed values to v to make the messages shorter so they don't wrap):

var msg;
var v = [1, 2, 3, 4, 5];
var temp = v[v.length - 1];
for (var i = v.length - 2; i >= 0; i--)
{
  msg = "Replacing v[" + i + " + 1] with v[" + i + "]: " + JSON.stringify(v);
  v[i + 1] = v[i];
  msg += " => " + JSON.stringify(v);
  console.log(msg);
}
msg = "Replacing v[0]     with temp: " + JSON.stringify(v);
v[0] = temp;
msg += " => " + JSON.stringify(v);
console.log(msg);

So

  • First, we grab the fifth value (5) into temp so we have it for later
  • Then, in the loop, we replace the fifth value (values[i + 1]) with the fourth value (values[i]), changing 1, 2, 3, 4, 5 into 1, 2, 3, 4, 4
  • We do it again, replacing the fourth with the third, changing 1, 2, 3, 4, 4 into 1, 2, 3, 3, 4
  • Keep doing that until we've replaced the second entry with the first, leaving us with 1, 1, 2, 3, 4
  • Outside the loop, put temp into the first place, giving us the end result 5, 1, 2, 3, 4
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Your in-depth steps are clear and precise! This have helped me a lot.
0

values = [1, 2, 3, 4, 5]

You take values[4] and put values[3] = 4 there, so the values[4] = 4 right now. What you don't understand here? I think you forgot about the fact that arrays are numbered starting from 0 not 1.

Comments

0

I think you have ignored these two statements:

int temp = values[values.length - 1];//Here temp = 5, last index element of your array

AND

values[0] = temp;// here 1 will be replaced by 5

Since in the loop last element is lost, they are storing it in a temporary variable temp pre-loop. Once the loop is that completed, that is 4th index replaced by 3rd, 3rd replaced by 2nd, 2nd replaced by 1st and 1st replaced by 0th index element.

After the loop is complete, your array would look something like this:

[1,1,2,3,4]

Now 0th index element is replaced with temp, which is the original 4th index of the array.

Now the array will be [5,1,2,3,4]

Makes sense now?

P.S: I would advise you to debug by putting more println statements in between to check whats happening. My suggestion is do the below in your code for better understanding:

System.out.println(Arrays.toString(values));
values[0] = temp;
System.out.println(Arrays.toString(values));

1 Comment

I'm not sure who downvoted but the two statements I do understand, I just didn't understand how the statement in the loop worked. But thanks anyway for helping!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.