public void shiftNTimes(int[] array, int numShifts) { int timesShifted = 0; while (timesShifted < numShifts) { int temp = array[0]; for (int i = 0; i < array.length  1; i++) { array[i] = array[i + 1]; } array[array.length  1] = temp; timesShifted++; } }
Shifting Array Elements Left
Page 1 of 13 Replies  23585 Views  Last Post: 22 April 2009  03:44 PM
#1
Shifting Array Elements Left
Posted 22 April 2009  01:28 PM
Can someone explain to me each step in this method that is supposed to shift each element to the left each time it is executed? I understand the while loop is checking the condition of the counter to verify how many times to run the for loop. The for loop confuses me with what it is doing. Take the example, array of ints {1, 2, 3, 4}. This shifted once to the left should result in {2, 3, 4, 1}. Now the temp variable in method is holding the 1 vaule in the original array. Now, the for loop runs first time and makes the modified array {1, 1, 3, 4} or {2, 2, 3, 4}? Since it is array[i] = array[i+1] makes the two array elements next to each other equal (in this example for i = 0). Now finally, the while look takes the temp value that was initially stored and replaces it with the last element of the array? Therefore, resulting in { 4, 1, 3, 1} or {4, 2, 3, 1}? Some detailed clarification of what this method actually does for the first time executed would be greatly appreciated! Thanks.
Replies To: Shifting Array Elements Left
#2
Re: Shifting Array Elements Left
Posted 22 April 2009  01:28 PM
Can someone explain to me each step in this method that is supposed to shift each element to the left each time it is executed? I understand the while loop is checking the condition of the counter to verify how many times to run the for loop. The for loop confuses me with what it is doing. Take the example, array of ints {1, 2, 3, 4}. This shifted once to the left should result in {2, 3, 4, 1}. Now the temp variable in method is holding the 1 value in the original array. Now, the for loop runs first time and makes the modified array {1, 1, 3, 4} or {2, 2, 3, 4}? Since it is array[i] = array[i+1] makes the two array elements next to each other equal (in this example for i = 0). Now finally, the while look takes the temp value that was initially stored and replaces it with the last element of the array? Therefore, resulting in { 4, 1, 3, 1} or {4, 2, 3, 1}? Some detailed clarification of what this method actually does for the first time executed would be greatly appreciated! Thanks.
public void shiftNTimes(int[] array, int numShifts) { int timesShifted = 0; while (timesShifted < numShifts) { int temp = array[0]; for (int i = 0; i < array.length  1; i++) { array[i] = array[i + 1]; } array[array.length  1] = temp; timesShifted++; } }
#3
Re: Shifting Array Elements Left
Posted 22 April 2009  01:50 PM
So say you have an array of {1,2,3,4}
You shift and it becomes 2,3,4,1 and shift again its 3,4,1,2??
Say you have an array of {2,3,4,5}
Would move 3 to index 0; 4 to index 1, 5 to index 2 and insert 2 at the last index.
You increase timesShifted every time it makes a shift, THe while loop has it at<nums times because at the end it will at the int that was at 0, to the very end. and increase numShifted.
Get it?
You shift and it becomes 2,3,4,1 and shift again its 3,4,1,2??
Say you have an array of {2,3,4,5}
Would move 3 to index 0; 4 to index 1, 5 to index 2 and insert 2 at the last index.
You increase timesShifted every time it makes a shift, THe while loop has it at<nums times because at the end it will at the int that was at 0, to the very end. and increase numShifted.
Get it?
#4
Re: Shifting Array Elements Left
Posted 22 April 2009  03:44 PM
Duplicate topic merged
Page 1 of 1
