你交换的是形式参数的值,并没有改变实际参数
public class swape
{
public static void main(String[] args)
{
int[] a = {1,2};
swap(0,1,a);
System.out.println(a[0] + " " + a[1]);
}
public static void swap(int n1, int n2, int a[])
{
int temp = a[n1];
a[n1] = a[n2];
a[n2] = temp;
}
} |