倒置字符串中各个字符位置
发布时间:2020-05-22 22:43:14 所属栏目:程序设计 来源:互联网
导读:/** * 倒置字符串中各个字符位置 * * @author Administrator * */public class Test_008 {public static void main(String[] args) {String str = abcdefghigk;char[] s = str.toCharArray();System.out.pr
/**
* 倒置字符串中各个字符位置
*
* @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] (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
