switching columns of a 2d array
so my problem is that i cannot get the elements of a 2d array to switch like i would be able to do in a single variable array. Ins开发者_JS百科tead of switching the elements they are just being continuously rewritten...
for (int column = 0; column < m[0].length; column++) {
shufcol = (int)(Math.random()*4);
temp = column;
System.out.println(shufcol);
for(int row = 0; row < m.length; row++) {
temp = row;
m[row][temp]=m[row][column];
m[row][column]= m[row][shufcol];
m[row][temp] = m[row][shufcol];
}
}
input array (3X4) {{0 1 2 3} {1 4 5 6} {0 7 8 9}}
output array {{2 2 3 2} {5 5 6 5} {8 8 9 8}}
If your curious about the math.random
, that is just to generate a random column between 0 to 3 to switch with. Again the issue is why is it only rewriting elements and not switching them?
I don't fully understand what you want to achieve at the end (because you haven't told), but I think, if you reread this piece of code:
temp = row;
m[row][temp]=m[row][column];
m[row][column]= m[row][shufcol];
m[row][temp] = m[row][shufcol];
several times and try to execute it with piece of paper and pen you'll find the mistake.
If I understood, this will switch the values in column and shufcol for all rows (I didn't test it):
for (int column = 0; column < m[0].length; column++) {
shufcol = (int)(Math.random()*4);
System.out.println(shufcol);
for(int row = 0; row < m.length; row++) {
temp = m[row][shufcol];
m[row][shufcol] = m[row][column];
m[row][column] = temp;
}
}
This will switch elements inside their rows:
//input array (3X4) {{0 1 2 3} {1 4 5 6} {0 7 8 9}}
int[][] m = {{0, 1, 2, 3}, {1, 4, 5, 6}, {0, 7, 8, 9}};
for (int column = 0; column < m[0].length; column++) {
int shufcol = (int)(Math.random()*4);
int shufrow = (int)(Math.random()*3); //need row to switch with, too
for(int row = 0; row < m.length; row++) {
if(row == shufrow){
int tempField =m[row][column];
m[row][column]= m[row][shufcol];
m[row][shufcol] = tempField;
System.out.println("In row " + row + " : column " + column + " was switched with column " + shufcol + "!");
break; //just switch once per row, easier debugging ^^-d
}
}
}
//print output array
for(int row = 0; row < m.length; row++) {
String line = "\n";
for (int column = 0; column < m[0].length; column++) {
line += m[row][column] + " ";
}
System.out.print(line);
}
Output:
In row 2 : column 0 was switched with column 2!
In row 0 : column 1 was switched with column 2!
In row 1 : column 2 was switched with column 3!
In row 0 : column 3 was switched with column 2!
0 2 3 1
1 4 6 5
8 7 0 9
Since i am not totally sure what you want to do, here is switching elements in the whole array randomly:
//input array (3X4) {{0 1 2 3} {1 4 5 6} {0 7 8 9}}
int[][] m = {{0, 1, 2, 3}, {1, 4, 5, 6}, {0, 7, 8, 9}};
for (int column = 0; column < m[0].length; column++) {
int shufcol = (int)(Math.random()*4);
int shufrow = (int)(Math.random()*3); //need row to switch with, too
//there is no point in duplicating the count variable of a loop!
System.out.println(shufcol);
for(int row = 0; row < m.length; row++) {
//check that random field is not current field!
if(row != shufrow && column != shufcol){
//switch current with random
int temp = m[row][column]; // le backuppe
m[row][column] = m[shufrow][shufcol]; // write le new value of "random source field" to current field
m[shufrow][shufcol] = temp; // write value of current field to "random source field"
System.out.println("switched [" + row + "][" + column + "] with [" + shufrow + "]["+ shufcol + "]");
break; // use break to switch only once per row, easier debugging ^^-d
}
else {
System.out.println("will not switch with self!");
}
}
}
//print output array
for(int row = 0; row < m.length; row++) {
String line = "\n";
for (int column = 0; column < m[0].length; column++) {
line += m[row][column] + " ";
}
System.out.print(line);
}
Yielding the following output:
2
switched [0][0] with [2][2]
0
switched [0][1] with [1][0]
0
switched [0][2] with [2][0]
2
switched [0][3] with [1][2]
8 1 0 5
1 4 3 6
2 7 0 9
Hope this helps! ^^-d
精彩评论