Results 1 to 14 of 14
Thread: Multidimensional array  swap
 03272011, 09:05 PM #1Member
 Join Date
 Mar 2011
 Posts
 8
 Rep Power
 0
Multidimensional array  swap
hey everybody!
My name is Mark and I'm new to Java, but (haha what a wonder) i already got my first problem ;)
The task is to write an method, which swaps a multidimensional array by 90°  eg.Java Code:1 2 3 4 will be oder better say should be 13 9 5 1 5 6 7 8 14 10 6 2 9 10 11 12 15 11 7 3 13 14 15 16 16 12 8 4
oh yea first task was to invert a regular array... which was kind a easy..
oh yea ² .. i am NOT allowed to use a second array to swap! i need to swap within the array..
Java Code:import java.util.Random; import java.util.Random; public class SwapIt { private static int length = 4; public static int[] changeArray(int[] array) { int temp = 0; Random number = new Random(); System.out.println("Initial Array:"); for (int i = 0; i < length; i++) { array[i] = number.nextInt() % 20; System.out.print("[" + array[i] + "]"); } System.out.println(""); System.out.println("Array after swapping:"); for (int i = array.length1; i >= 0; i) { temp = array[i]; array[i] = temp; System.out.print("[" + array[i] + "]"); } return array; } public static int[][] changeArray(int[][] array) { Random number = new Random(); System.out.println("randomized array:"); for (int i = 0; i <array.length; i++ ) { for (int j =0; j<array.length;j++) { array[i][j] = number.nextInt() % 20; System.out.print(array[i][j] + "\t"); } System.out.println(); } System.out.println(); System.out.println("new array:"); int temp1 = 0; int temp2 = 0; int row = array.length; int column = 0; for (int i = 0; i <array.length; i++ ) { for (int j =0; j<array.length;j++) { // for (int i = array.length1; i >=0; i ) { // for (int j =array.length1; j>=0;j) { temp1 = array[row][column]; array[i][j]=temp1; row; System.out.print(""+array[i][j]); } } // array[i][j]=temp1; // HERE I THOUGHT SOMETHING ABOUT LIKE PUTTING ROW AND COLLUMN //TO 0 AGAIN .. BUT IT DIDN'T WORK THE WAY I WANTED :) // System.out.print(temp1 + "\t"); return array; } public static void main(String[] args) { int[] array = new int[length]; changeArray(array); System.out.println(); System.out.println(); // creating [][] array int arrayd[][] = new int[length][length]; changeArray(arrayd); System.out.println(); } }
Mark
 03272011, 10:03 PM #2Member
 Join Date
 Mar 2011
 Posts
 7
 Rep Power
 0
e=0;
for(int c=array.length1;c>=0;c){
f=0;
for(int d=array.length1;d>=0;d){
b[e][f]=a[c][d];
f++}
e++}
mey help
 03272011, 10:12 PM #3Member
 Join Date
 Mar 2011
 Posts
 8
 Rep Power
 0
 03272011, 11:10 PM #4Senior Member
 Join Date
 Nov 2010
 Posts
 210
 Rep Power
 7
Maybe the XOR swap algorithm?
 03282011, 09:40 AM #5Member
 Join Date
 Mar 2011
 Posts
 8
 Rep Power
 0
 03282011, 09:44 AM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,373
 Blog Entries
 7
 Rep Power
 25
 03282011, 10:26 AM #7Member
 Join Date
 Mar 2011
 Posts
 8
 Rep Power
 0
ahm .. very interesting approach.
i know how to reverse reach row of the matrix, but atm i just can't figuere out how to transpose it, so that the last step would be to reverse it.
can u give me a hint?
i retyped the matrix  for optical reason
Java Code:1 2 3 4 will be oder better say should be 13 9 5 1 5 6 7 8 14 10 6 2 9 10 11 12 15 11 7 3 13 14 15 16 16 12 8 4
Java Code:EDIT: u mean like? 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 .. but how is this possible without a second array?
Last edited by BeginnerNoob; 03282011 at 10:44 AM.
 03282011, 10:53 AM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,373
 Blog Entries
 7
 Rep Power
 25
 03282011, 11:16 AM #9Member
 Join Date
 Mar 2011
 Posts
 8
 Rep Power
 0
hmm.. i exactly know what u mean, but it doesn't work, because I tryed it this way...
Java Code:for (int i = 0; i <array.length; i++ ) { for (int j = 0; j<array.length;j++) { array[i][j] = array[j][i]; System.out.print(" "+array[i][j]); }System.out.println(); }
 03282011, 11:24 AM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,373
 Blog Entries
 7
 Rep Power
 25
 03282011, 12:01 PM #11Member
 Join Date
 Mar 2011
 Posts
 8
 Rep Power
 0
im getting closer... but still not working well :D
Java Code:for (int i = 0; i <array.length; i++ ) { for (int j =0; j<array.length;j++) { if(i==j) { array[i][j]=array[i][j]; } if(i<j){ array[i][j] = array[j][i]; } // if(i>j) { // array[i][j]=array[i][j]; // } System.out.print(" "+array[i][j]); }System.out.println(); }
how can i merge the i<j and the j>i that it works the way i want i to work :)
Thanks a lot for your help so far!
Mark
 03282011, 12:43 PM #12
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,373
 Blog Entries
 7
 Rep Power
 25
Don't overcomplicate things:
Java Code:for (int j= 0; j < matrix.length; j++) for (int i= 0; i < j; i++) { // < pay attention to the upperbound // swap matrix[j][i] and matrix[i][j] } }
JosThe only person who got everything done by Friday was Robinson Crusoe.
 03282011, 06:43 PM #13Member
 Join Date
 Mar 2011
 Posts
 8
 Rep Power
 0
puh im sorry.. it doesn't wok at all. maybe i'm to stupid
I debugged it, and saw, that the first index which will be printed is j = 1; .. so i changed the code to i<=j and i changed ...Java Code:for (int j = 0; j <array.length; j++ ) { for (int i = 0; i<=j;i++) { array[j][i]=array[i][j]; System.out.print(" "+array[i][j]); }System.out.println(); }
Java Code:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 new array: 1 2 6 3 7 11 4 8 12 16
u have any more ideas for this bunch of cr** ;)
it would be so damn easy when I'm allowed to use a second array.. but hey.. that's the way it is :D .... was just to motivate myself a bit :D
.. now the left side of the diagonal is working .. but the right one isn't anymore !Last edited by BeginnerNoob; 03282011 at 06:53 PM.
 03302011, 03:02 PM #14Member
 Join Date
 Mar 2011
 Posts
 8
 Rep Power
 0
Similar Threads

Filling a multidimensional array, and then change it
By MaceMan in forum New To JavaReplies: 2Last Post: 03242011, 11:43 PM 
initialize multidimensional array
By Aidoru in forum New To JavaReplies: 2Last Post: 12042010, 02:00 PM 
Retrieve Multidimensional Array ??
By oneofthelions in forum New To JavaReplies: 3Last Post: 12122009, 08:24 AM 
Navigate through a multidimensional array
By VinTiger in forum New To JavaReplies: 4Last Post: 05052009, 07:16 PM 
[SOLVED] Multidimensional array
By Torgero in forum New To JavaReplies: 20Last Post: 03232009, 12:36 AM
Bookmarks