3 Replies - 22282 Views - Last Post: 22 April 2009 - 03:44 PM Rate Topic: -----

#1 pwnstar89  Icon User is offline

  • New D.I.C Head

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

Shifting Array Elements Left

Post icon  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  Icon User is offline

  • New D.I.C Head

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

Re: Shifting Array Elements Left

Post icon  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++;
		}
	} 

Was This Post Helpful? 0
  • +
  • -

#3 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Shifting Array Elements Left

Posted 22 April 2009 - 03:44 PM

Duplicate topic merged
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1