Results 1 to 5 of 5
  1. #1
    whit3ang3l is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Swap 2 dimensional Array

    Hi, i have got a problem with 2 dimensional array

    int[][] box = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

    |1 2 3|
    |4 5 6|
    |7 8 9|
    then i want to swap number 8 with 7 with method
    but, i want that swap happen only in method.
    My friend told me Array in java is object, so if i use method, the variable box on method parameter and int box on the main will have same memory adress
    If I swap 7 8 on the method, it change the box in the main too.

    so how to swap it without change the original array in main ?
    he told me to use
    copyArray[][] = box.clone();
    then put copyArray into method parameter
    but it doesn't work.
    box contents was still changing.

    Please help me thanks
    Last edited by whit3ang3l; 03-04-2012 at 07:39 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Swap 2 dimensional Array

    Your requirement is not that much clear to me. Do you want to swap number 7 and 8 in that array? Only that, not others?

  3. #3
    whit3ang3l is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: Swap 2 dimensional Array

    Quote Originally Posted by Eranga View Post
    Your requirement is not that much clear to me. Do you want to swap number 7 and 8 in that array? Only that, not others?
    I want to swap number 7, 8 in that array without changing the array in the main
    example :

    Java Code:
    public class Test {
    	public static void main(String[] args) {
    		int array[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    		int copyArray[][] = array.clone();
    		printArray(array);
    		swap(copyArray);
    		printArray(array);
    		
    	}
    	public static void swap(int[][] arr) {
    		int temp = arr[2][1];
    		arr[2][1] = arr[2][0];
    		arr[2][0] = temp;
    	}
    	public static void printArray(int[][] arr) {
    		for(int a = 0; a < 3; a++) {
    			for(int b = 0; b < 3; b++)
    				System.out.print(arr[a][b] + " ");
    			System.out.println();
    		}
    		System.out.println();
    	}
    
    }
    the original array on main also changed
    I want swap only arr[][] in the parameter function, not the array in the main.

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

    Default Re: Swap 2 dimensional Array

    Cloning an array doesn't make a deep copy of the array; also make copies of each row for a 'deep' copy.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 2
    Last Post: 02-13-2012, 06:39 AM
  2. Swap array value
    By irnie1994 in forum New To Java
    Replies: 1
    Last Post: 11-10-2011, 05:06 PM
  3. how can i loop my array swap
    By belfast09 in forum New To Java
    Replies: 3
    Last Post: 06-15-2011, 04:35 AM
  4. Multidimensional array - swap
    By BeginnerNoob in forum New To Java
    Replies: 13
    Last Post: 03-30-2011, 03:02 PM
  5. 2 dimensional array
    By sehudson in forum New To Java
    Replies: 5
    Last Post: 02-20-2011, 11:56 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
  •