以下程序交换数组中的两个元素。但它似乎没有运行,但我确信我没有做错任何事。
public class swap {
public static String[] swap(int first, int second, String[] A) {
String hold = A[first];
A[first] = A[second];
A[second] = hold;
return A;
}
public static void main(String[] args) {
String[] B = {"Dawn", "Justice", "Of"};
String[] c = swap.swap(1, 2, B);
System.out.print(c);
}
}
答案 0 :(得分:0)
1。)需要创建swap class
的对象,或者因为static
不需要创建对象,只需使用swap(1, 2, B);
2。)打印Arrays.toString(c)
修改后的代码
public static String[] swap(int first, int second, String[] A) {
String hold = A[first];
A[first] = A[second];
A[second] = hold;
return A;
}
public static void main(String[] args) {
String[] B = { "Dawn", "Justice", "Of" };
String[] c = swap(1, 2, B);
System.out.print(Arrays.toString(c));
}
<强>输出强>
[Dawn, Of, Justice]
答案 1 :(得分:-1)
import java.util.Arrays; 公共班级交换{
public static String[] swap(int first, int second, String[] A) {
String hold = A[first];
A[first] = A[second];
A[second] = hold;
return A;
}
public static void main(String[] args) {
String[] B = {"Dawn", "Justice", "Of"};
String[] c = swap.swap(1, 2, B);
System.out.print(Arrays.toString(c));
}
}