Results 1 to 3 of 3
  1. #1
    lynkxs is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default shifting 2d array

    i have a problem with the shifting. whenever i shift my table to the left, instead of the leftmost column moving to the very last column, the last column (before the shifting) seems to copy to the last one.

    Java Code:
    import java.util.Scanner;
    public class Shifting {
    	public static void main(String[] args) {
    		
    		Scanner input = new Scanner(System.in);
    		
    		System.out.print("Enter the size of the rows and columns: ");
    		int rowsColumns = input.nextInt();
    		
    		int[][] table = new int[rowsColumns][rowsColumns];
    		
    		//Creates the matrix
    		for(int i = 0; i < table.length; i++) {
    			for(int j = 0; j < table[i].length; j++) {
    			table[i][j] = 1 + (int)(Math.random()*((9-1) + 1));
    			}
    		}
    		
    		System.out.println();
    		printTable(table);
    		System.out.println();
    		
    		System.out.print("1. Shift left\n2. Shift right\n3. Shift up\n4. Shift down\n5. Exit program\nChoose an option (1 ~ 5): ");
    		int userInput = input.nextInt();
    		
    		System.out.println();
    		
    		switch(userInput) {
    			
    			case 1: shiftLeft(rowsColumns, table);
    					printTable(table);
    					break;
    			default: System.exit(0);
    		}
    	
    	}
    	
    	public static void printTable(int[][] table) {
    
    		for(int i = 0; i < table.length; i++) {
    			for(int j = 0; j < table.length; j++) {
    			System.out.print(table[i][j] + " ");
    			}
    			System.out.println();
    		}
    	}
    	
    	public static void shiftLeft(int rowsColumns, int[][] table) {
    		
    		int left = table[0][0];
    		for(int i = 0; i < table.length; i++) {
    			for(int j = 0; j < table.length - 1; j++) {
    				table[i][j] = table[i][j + 1];
    				left = table[i][j];
    			}
    		}
    	}
    }
    this is my output:
    Java Code:
    Enter the size of the rows and columns: 5
    
    2 5 1 3 2 
    8 6 8 8 7 
    7 8 2 1 8 
    6 1 9 6 8 
    5 8 5 8 4 
    
    1. Shift left
    2. Shift right
    3. Shift up
    4. Shift down
    5. Exit program
    Choose an option (1 ~ 5): 1
    
    5 1 3 2 2 
    6 8 8 7 7 
    8 2 1 8 8 
    1 9 6 8 8 
    8 5 8 4 4

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

    Default Re: shifting 2d array

    Before you start shifting everything to the left you have to keep the leftmost element somewhere and stick it back in as the rightmost element after the shifting is done.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    lynkxs is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Shifting an Array
    By Zaibatsu in forum New To Java
    Replies: 2
    Last Post: 12-14-2011, 12:02 AM
  2. Problem with shifting array elements
    By holy.water in forum New To Java
    Replies: 4
    Last Post: 02-25-2011, 08:56 PM
  3. Q about shifting data in an array
    By alihht in forum New To Java
    Replies: 7
    Last Post: 02-03-2010, 06:17 AM
  4. Shifting characters in array
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 04-24-2009, 10: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
  •