/** * 倒置字符串中各个字符位置 * * @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,k] 倒置后:[k,a]原文链接:https://www.f2er.com/javaschema/286475.html