将“hello world” 输出为“world hello”

来源:互联网 发布:四年级数学优化 编辑:程序博客网 时间:2024/05/16 12:30
public class Reverset1{
//将“hello world” 输出为“world hello”
public static void main(String[] args) {
String s="hello world!";
System.out.println(change(s));
}
public static String change(String s){
//使用String工具类将字符串进行分解
StringTokenizer st = new StringTokenizer(s);
ArrayList<Object> list = new ArrayList<Object>();
while(st.hasMoreElements()){
list.add(st.nextElement());
}

StringBuffer s2 = new StringBuffer();
int j=0;
for (int i = list.size()-1; i >=0; i--) {
s2.append(list.get(i)+" ");
}
return s2.toString();
}
}
public class Reverset2{
//将“hello world” 输出为“world hello”
public static void main(String[] args) {
String s="hello world!";
System.out.println(change(s));
}
public static String change(String s){

StringBuffer sf = new StringBuffer();0

//利用字符串的分割

String[] s2 = s.split(" ");
for (int i = s2.length-1; i >=0; i--) {
sf.append(s2[i]+" ");
}
return sf.toString();
}
}
原创粉丝点击