# help with shifting elelemnts in arrays

• 11-22-2012, 08:30 AM
zerocool1822
help with shifting elelemnts in arrays
hello , ive been at this for 2 days now and i just cant get it. im trying to figure out how to shift The columns of a table one position to the left im at the point of giving up but i thought id atleast try to get some help online. my method is below. as u can see , its a mess , as ive been trying to tinker with it and get it to shift the right direction , but its not happening.

Code:

```public static void shiftRight(int[][] table){                        for(int i = table.length-1; i > 0; i--){                         table[0]=table[i-1];                                }                 table[0]=table[table.length-1];                         }```
• 11-22-2012, 08:41 AM
JosAH
Re: help with shifting elelemnts in arrays
First we have to agree on a few things: columns run from left to right and rows run from top to bottom, right? A table element (row, col) is stored in a two dimensional array[row][col], right? If a bunch of columns is moved to the left, what happens to the column at the far right? What happens to the column that is 'shifted away'?

kind regards,

Jos
• 11-22-2012, 08:47 AM
zerocool1822
Re: help with shifting elelemnts in arrays
ok , yes i agree with what you said and understand that. the column at the far right moves over 1 to the left. and the column thats shifted away moves over 1 to the left as well , so its like they rotate to the left as they get shifted.
• 11-22-2012, 08:50 AM
JosAH
Re: help with shifting elelemnts in arrays
Quote:

Originally Posted by zerocool1822
ok , yes i agree with what you said and understand that. the column at the far right moves over 1 to the left. and the column thats shifted away moves over 1 to the left as well , so its like they rotate to the left as they get shifted.

Ah, ok, so you want to rotate all columns one position, e.g. A B C D E --> B C D E A, correct?

kind regards,

Jos
• 11-22-2012, 08:53 AM
zerocool1822
Re: help with shifting elelemnts in arrays
yes so like
9 3 0 9 8
7 1 5 6 3
0 8 0 6 8
1 6 8 7 5
3 0 4 4 8

shifted to the left would be
3 0 9 8 9
1 5 6 3 7
8 0 6 8 0
6 8 7 5 1
0 4 4 8 3
• 11-22-2012, 09:00 AM
JosAH
Re: help with shifting elelemnts in arrays
Yep, that's rotating to the left alright; for each element i in an array, you want to plug a value in and take a value out of it (to be plugged in somewhere else):

Code:

```takeout= array[i]; array[i]= plugin; plugin= takeout;```
For the rightmost element you want to plug in the leftmost element, i.e. array[0], so your code becomes:

Code:

```plugin= array[0]; for (int i= array.length-1; i >= 0; i--) {   takeout= array[i];   array[i]= plugin;   plugin= takeout; }```
This code rotates a single one dimensional array; a table is just an array of one dimensional arrays, so you have to perform the above code on every row of your table ...

kind regards,

Jos
• 11-22-2012, 09:19 AM
zerocool1822
Re: help with shifting elelemnts in arrays
when u say take out , you mean extracting an elements from the column right? so its like , take out an element from table[i]
• 11-22-2012, 09:28 AM
JosAH
Re: help with shifting elelemnts in arrays
Quote:

Originally Posted by zerocool1822
when u say take out , you mean extracting an elements from the column right? so its like , take out an element from table[i]

Yup, just before you plug in a new element at a position in the array, you have to take out the previous value (to plug it in at another position). Follow the code step by step for a small example array (say, three elements) and see how it works.

kind regards,

Jos
• 11-22-2012, 09:58 AM
zerocool1822
Re: help with shifting elelemnts in arrays
this is what i came up with, it works in theory lol, i was using modulus to extract the numbers. but ofcourse it doenst perform right when i run it

Code:

```public static void shiftRight(int[][] table){                                 for (int i= table.length-1; i >= 0; i--) {                         table[(i+1)%table[i].length]=table[i];                         table[i]=table[0];                         table[0]=table[(i+1)%table[i].length];                                                                                                 }                 }```
• 11-22-2012, 11:01 AM
JosAH
Re: help with shifting elelemnts in arrays
Sorry to say so, but it doesn't work in my theory; try it with a small table (two dimensional array) and see for yourself ...

kind regards,

Jos