倒置字符串中各个字符位置
发布时间:2020-12-13 19:58:41 所属栏目:百科 来源:网络整理
导读:/** * 倒置字符串中各个字符位置 * * @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(
/** * 倒置字符串中各个字符位置 * * @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] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |