我写了一个function,将比如abcd中c移动到指定位置,其余往后一个index
举例: swap(abcd,2,0) 则输出 -> cabd
代码如下
public class why {
static public void main(String args[]) { char[] chs = {'a','b','c','d'}; sw( chs ,2 , 0 ); } static public void sw(char chs[] , int i , int j) { char [] tempchs = chs ; //将abcd存入tempchs System.out.print("start "); System.out.println(tempchs);/*-----------------------------------------------------*/ char temp = chs[i]; for(int k = i ; k>j ; k--) { chs[k] = chs[k-1]; } chs[j] = temp ; /*-----------------------------------------------------*/ System.out.print("end "); System.out.println(tempchs); }
}
执行结果
start abcd
end cabd
我想问的问题是,为何我进入function之前,已经用tempchs将输入的abcd暂存,但当它将abcd成功变为cabd时,我的tempchs却改变了呢?
改变的结果,不是应该只改变了chs本身吗?
鸿蒙传说
相关分类