# Thread: help with shifting elelemnts in arrays

1. Member
Join Date
Nov 2012
Posts
9
Rep Power
0

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

Java 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];

}```
Last edited by zerocool1822; 11-22-2012 at 10:05 AM.

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

3. Member
Join Date
Nov 2012
Posts
9
Rep Power
0

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

4. ## Re: help with shifting elelemnts in arrays

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

5. Member
Join Date
Nov 2012
Posts
9
Rep Power
0

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

6. ## 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):

Java 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:

Java 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

7. Member
Join Date
Nov 2012
Posts
9
Rep Power
0

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

8. ## Re: help with shifting elelemnts in arrays

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

9. Member
Join Date
Nov 2012
Posts
9
Rep Power
0

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

Java 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];

}
}```
Last edited by zerocool1822; 11-22-2012 at 10:06 AM.

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•