Results 1 to 10 of 10
- 11-22-2012, 07:30 AM #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 09:05 AM.
- 11-22-2012, 07:41 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-22-2012, 07:47 AM #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.
- 11-22-2012, 07:50 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 11-22-2012, 07:53 AM #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
- 11-22-2012, 08:00 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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):
For the rightmost element you want to plug in the leftmost element, i.e. array[0], so your code becomes:Java Code: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 ...Java Code:plugin= array[0]; for (int i= array.length-1; i >= 0; i--) { takeout= array[i]; array[i]= plugin; plugin= takeout; }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-22-2012, 08:19 AM #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]
- 11-22-2012, 08:28 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: help with shifting elelemnts in arrays
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-22-2012, 08:58 AM #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 09:06 AM.
- 11-22-2012, 10:01 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Shifting Hex Values
By Kyle227 in forum Advanced JavaReplies: 10Last Post: 01-09-2012, 10:59 PM -
Shifting an Array
By Zaibatsu in forum New To JavaReplies: 2Last Post: 12-14-2011, 12:02 AM -
Letter-Shifting Program
By jhurley03 in forum New To JavaReplies: 2Last Post: 10-13-2011, 07:53 AM -
Shifting method using >>
By ile4 in forum New To JavaReplies: 7Last Post: 11-09-2010, 06:19 PM -
[SOLVED] Shifting an array
By VeasMKII in forum New To JavaReplies: 2Last Post: 02-04-2009, 06:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks