我只是想用字符数组做一些事情。当我反转它时,每个原始字符后面都会出现一个空格。为什么 ?我的代码片段是这样的
public class StringReverseChar {
//REVERSE A STRING CHARACTER BY CHARACTER
static String mStr=".gnihtemos od ot gniyrT ma I";
int len;
int topOfS;
char[] myReverse=new char[75];
public StringReverseChar() {
topOfS=-1;
myReverse=new char[70];
}
public static void main(String ar[]){
StringReverseChar mObj=new StringReverseChar();
int len=mObj.calLen(mStr);
for(int i=0;i<len;i++)
mObj.pushChar(mStr.charAt(i));
// System.out.println("The Reverse is:");
while(mObj.topOfS !=-1)
System.out.print(mObj.popChar());
}
private char popChar() {
return myReverse[topOfS--];
}
private void pushChar(char c) {
myReverse[++topOfS]=c;
System.out.print(myReverse[topOfS++]);
}
private int calLen(String mStr2) {
return mStr2.length();
}
}
我的输出是这样的: 反过来是: I amtryingtodosomething 。