/**
* 倒置字符串中各个字符位置
*
* @author Administrator
*
*/
public class Test_008 {
public static void main(String[] args) {
String str = "abcdefghigk";
char[] s = str.toCharArray();
System.out.println("倒置前:" + Arrays.toString(s));
reverse(s);
System.out.println("倒置后:" + Arrays.toString(s));
}
static void reverse(char[] s) {
char temp;
for (int i = 0, j = s.length - 1; i < j; i++, j--) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
输出结果:
倒置前:[a, b, c, d, e, f, g, h, i, g, k]
倒置后:[k, g, i, h, g, f, e, d, c, b, a]