Results 1 to 5 of 5
Thread: Swap 2 dimensional Array
- 03-04-2012, 07:21 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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 thanksLast edited by whit3ang3l; 03-04-2012 at 07:39 AM.
- 03-04-2012, 08:13 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 03-04-2012, 08:51 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Re: Swap 2 dimensional Array
I want to swap number 7, 8 in that array without changing the array in the main
example :
the original array on main also changedJava 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(); } }
I want swap only arr[][] in the parameter function, not the array in the main.
- 03-04-2012, 10:44 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,397
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-06-2012, 12:53 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Swap 2 dimensional Array
A shallow copy, isn't it...
Similar Threads
-
How to access the array elements in an Arraylist of Arrays and then swap the sublist?
By udayjava in forum New To JavaReplies: 2Last Post: 02-13-2012, 06:39 AM -
Swap array value
By irnie1994 in forum New To JavaReplies: 1Last Post: 11-10-2011, 05:06 PM -
how can i loop my array swap
By belfast09 in forum New To JavaReplies: 3Last Post: 06-15-2011, 04:35 AM -
Multidimensional array - swap
By BeginnerNoob in forum New To JavaReplies: 13Last Post: 03-30-2011, 03:02 PM -
2 dimensional array
By sehudson in forum New To JavaReplies: 5Last Post: 02-20-2011, 11:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks