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(); } } |
将“hello world” 输出为“world hello”
最新推荐文章于 2024-04-26 17:51:23 发布