Results 1 to 10 of 10
  1. #1
    zerocool1822 is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    zerocool1822 is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default 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. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: help with shifting elelemnts in arrays

    Quote Originally Posted by zerocool1822 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    zerocool1822 is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default 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. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    zerocool1822 is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default 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. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: help with shifting elelemnts in arrays

    Quote Originally Posted by zerocool1822 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    zerocool1822 is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

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

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Shifting Hex Values
    By Kyle227 in forum Advanced Java
    Replies: 10
    Last Post: 01-09-2012, 10:59 PM
  2. Shifting an Array
    By Zaibatsu in forum New To Java
    Replies: 2
    Last Post: 12-14-2011, 12:02 AM
  3. Letter-Shifting Program
    By jhurley03 in forum New To Java
    Replies: 2
    Last Post: 10-13-2011, 07:53 AM
  4. Shifting method using >>
    By ile4 in forum New To Java
    Replies: 7
    Last Post: 11-09-2010, 06:19 PM
  5. [SOLVED] Shifting an array
    By VeasMKII in forum New To Java
    Replies: 2
    Last Post: 02-04-2009, 06:18 PM

Posting Permissions

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