# Shifting Array Elements Left

Page 1 of 1

## 3 Replies - 45765 Views - Last Post: 22 April 2009 - 03:44 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=101056&amp;s=370d681a213f94e4635deff1053f830c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 pwnstar89

Reputation: -1
• Posts: 16
• Joined: 22-April 09

# 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.

``` 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++;
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Shifting Array Elements Left

### #2 pwnstar89

Reputation: -1
• Posts: 16
• Joined: 22-April 09

## 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 Fuzzyness

• Comp Sci Student

Reputation: 669
• Posts: 2,438
• Joined: 06-March 09

## 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?

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Shifting Array Elements Left

Posted 22 April 2009 - 03:44 PM

Duplicate topic merged