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.

U.Smith
  • 3
  • 1
  • 4
  • 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. – T.J. Crowder Oct 19 '16 at 09:46
  • I have but i still don't understand. – U.Smith Oct 19 '16 at 09:47
  • this is nothing fancy, if you want to know more, just print the array content in loop after every iteration. – Mritunjay Oct 19 '16 at 09:48
  • [Visualised Example](http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=public+class+ClassNameHere+%7B%0A+++public+static+void+main(String%5B%5D+args)+%7B%0A++++++int%5B%5D+values+%3D+new+int%5B%5D%7B1,+2,+3,+4,+5%7D%3B%0A++++++int+temp+%3D+values%5Bvalues.length+-+1%5D%3B%0A++++++for+(int+i+%3D+values.length+-+2%3B+i+%3E%3D+0%3B+i--)+%7B%0A++++++++++values%5Bi+%2B+1%5D+%3D+values%5Bi%5D%3B%0A++++++%7D%0A++++++values%5B0%5D+%3D+temp%3B%0A++++++for+(int+i+%3A+values)+%7B%0A+++++++++++++System.out.print(i+%2B+%22+%22)%3B%0A++++++%7D%0A+++%7D%0A%7D&mode=display&curInstr=0) – d.j.brown Oct 19 '16 at 09:49
  • Checkout this link http://stackoverflow.com/questions/7970857/java-shifting-elements-in-an-array – Mark Brown Oct 19 '16 at 09:50
  • Maybe add your println statement to your for loop before and after the `values[i + 1]` part and see what changes and print out the values of i as well. – Ash Oct 19 '16 at 09:51
  • you correctly described what happens but your conclusion is wrong. the loop runs in revers from highest index to lowest. therefore, in the first iteration, it copies the value `4` at position `[3]` to the last position `[3+1]` overwriting the `5`. in the next iteration it copies the `3` at position `[2]` to position `[2+1]` overwriting the `4` in there... – Timothy Truckle Oct 19 '16 at 09:52

3 Answers3

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
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
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.

Shadov
  • 5,421
  • 2
  • 19
  • 38
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));
  • 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! – U.Smith Oct 19 '16 at 10:01