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

  • Post author:
  • Post category:其他
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();
}
}


版权声明:本文为zhangwen809877665原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。