package str.demo;
public class ReverseStr {
public static void main(String[] args) {
System.out.println(reverse("abcdefg"));
}
public static String reverse(String data) {
char[] arr = data.toCharArray();
int len = data.length();
for (int i = 0; i < len / 2; i++) {
swap(arr, i, len - i - 1);
}
return new String(arr);
}
private static void swap(char[] arr, int index1, int index2) {
char temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
}
这个代码打印的结果是 gfedcba,我想请教一下, 如何让打印的结果为GefDcba呢?
请高手指教、谢谢
|
|