分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请轻击http://www.captainbed.net
package live.every.day.Programming.String;
/**
* 题目:
* 字符串的调整与替换:将所有的"*"字符挪到左边,数字字符挪到右边。
*
* 思路:
* 从右向左倒着复制。
*
* @author Created by LiveEveryDay
*/
public class StringAdjustmentAndReplacement2 {
public static void modify(char[] a) {
if (a == null || a.length == 0) {
return;
}
int j = a.length - 1;
for (int i = a.length - 1; i > -1; i--) {
if (a[i] != '*') {
a[j--] = a[i];
}
}
while (j > -1) {
a[j--] = '*';
}
}
public static void main(String[] args) {
char[] a = {'1', '2', '*', '3', '*', '*', '4', '5'};
modify(a);
System.out.printf("The modified character array is: %s", new String(a));
}
}
// ------ Output ------
/*
The modified character array is: ***12345
*/