以下是我的代码。在这里,我必须切换2D数组中的两个名称,但我不知道如何做到这一点。
任何人都知道该怎么办?
import java.util.Scanner;
public class Homeworktest {
public static void main(String[] args) {
String[][] people = new String[3][3];
people[0][0] = "April";
people[0][1] = "Jenny";
people[0][2] = "Charlie";
people[1][0] = "Maya";
people[1][1] = "Daniel";
people[1][2] = "Felix";
people[2][0] = "Jack";
people[2][1] = "Charlotte";
people[2][2] = "Nick";
for(int i = 0; i < people.length; i++) {
String[] subarrays = seatingChart[i];
for(int y = 0; y < people.length; y++) {
System.out.print(subarrays[y] + " ");
}
System.out.println();
}
public static void switchSeats(int row1, int col1, int row2, int col2) {
}
}
}
答案 0 :(得分:1)
要交换两个数组位置的值,首先,在临时变量中保存一个值,将第二个位置的值分配给第一个位置,最后将临时变量的值分配给第二个位置。
public static void switchSeats(String[][] people, int row1,
int col1, int row2, int col2) {
String tmp = people[row1][col1];
people[row1][col1] = people[row2][col2];
people[row2][col2] = tmp;
}
在上面的代码中,tmp
用作临时变量。