日期:2014-05-20  浏览次数:20956 次

面试题目求助~~!
String   s= "你好   JAVA   HELLO   WORLD ";
写一程序输出结果为 "WORLD   HELLO   JAVA   你好 ";   不能用数组.大家帮忙想想好办法

------解决方案--------------------
public class MyTest{

public static void main(String[] args) {
System.out.println( "WORLD HELLO JAVA 你好 ");;
}
}
------解决方案--------------------
用个栈就行了嘛。
一个简单的伪码算法如下
f(String str){
String word= " ";
while (word!=null)
{
if word不是空串
stack.push(当前的word);
read next word from str;
}
从栈顶到栈底打印word
}
------解决方案--------------------
public class MyTest{

public static void main(String[] args) {
System.out.println( "WORLD HELLO JAVA 你好 ");;
}
}


==============================================================
正点!!!
------解决方案--------------------
晕他不让你用数组你不会用个结果集啊
------解决方案--------------------
把 "你好 " 截下来,加到一个new string里去
再把 "JAVA " 截下来,加到string前面去
重复
最后打印
------解决方案--------------------
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;

public class ReverseWord {
public static void main(String[] args){
String s = "你好 JAVA HELLO WORLD ";
StringTokenizer st = new StringTokenizer(s);

Stack <String> vs = new Stack <String> ();

while(st.hasMoreTokens())
vs.push(st.nextToken());

while(!vs.empty())
System.out.print(vs.pop() + " ");


}
}
输出:WORLD HELLO JAVA 你好
------解决方案--------------------
"写一程序输出结果为 "WORLD HELLO JAVA 你好 "; 不能用数组.大家帮忙想想好办法 "

String s= "你好 JAVA HELLO WORLD ";
s = "WORLD HELLO JAVA 你好 ";
System.out.println(s);

------解决方案--------------------
String str= "你好 JAVA HELLO WORLD ";
String[] strTemp=str.split( " ");
String result= " ";
for(String temp:strTemp){
result=temp+ " "+result;
}
System.out.println(result.substring(0, result.length()-1));
这样该行?
------解决方案--------------------
hansfly
的解决方法is good
------解决方案--------------------
测试过的,正确
import java.util.*;
public class StringTest{

public static void main(String[] arg){
String hello = "你好 JAVA HELLO WORD ";
StringBuffer str = new StringBuffer(hello);
StringBuffer result = new StringBuffer( " ");
StringTokenizer st = new StringTokenizer(hello);
while (st.hasMoreTokens()) {
String strTest = st.nextToken();
result.insert(0, " ");
result.insert(0,strTest);
}
System.out.println(result.toString());

}
}