所以我的问题是,我不能像在单个变量数组中那样切换二维数组的元素。而不是切换元素,他们只是不断重写......
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];
}
}
输入数组(3X4) {{0 1 2 3} {1 4 5 6} {0 7 8 9}}
输出数组 {{2 2 3 2} {5 5 6 5} {8 8 9 8}}
如果您对math.random
感到好奇,那就是生成0到3之间的随机列来切换。问题是为什么它只是重写元素而不是切换元素?
答案 0 :(得分:1)
我不完全明白你最终想要达到的目标(因为你还没有被告知),但我想,如果你重读这段代码:
temp = row;
m[row][temp]=m[row][column];
m[row][column]= m[row][shufcol];
m[row][temp] = m[row][shufcol];
多次尝试用纸和笔执行它你会发现错误。
答案 1 :(得分:0)
如果我理解,这将切换列和shufcol中所有行的值(我没有测试它):
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;
}
}
答案 2 :(得分:0)
这会切换行内的元素:
foreach ($request['array'] as $key => $value) {
if(DB::table('users')->where('id',$value)->where('auth',0)->exists() == true){
DB::table('users')->where('id', $value)->update(['auth_teacher' => 1]);
}
}
输出:
//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);
}
由于我不完全确定你想做什么,这里是随机切换整个数组中的元素:
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
产生以下输出:
//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);
}
希望这有帮助! ^^ - d