I wrote a function, to move c such as abcd to the specified location, and the rest to an index
example: swap (abcd,2,0), then output-> cabd
the code is as follows
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 ; //abcdtempchs
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);
}
}
execution result
start abcd
end cabd
the question I want to ask is, before I entered function, I used tempchs to temporarily store the entered abcd, but when it successfully turned abcd into cabd, my tempchs changed?
the result of the change, shouldn"t it only change the chs itself?