JAVA 通过字符串的split方法,按照空格拆分字符串,之后按首字母排序 10
展开全部
public static void main(String args[])
{
String target="John Bill Peter ";
ArrayList<String> list=new ArrayList<String>();
for(String temp:target.split(" "))
{
list.add(temp);
}
Collections.sort(list);
for(String temp:list)
{
System.out.print(temp+" ");
}
}
{
String target="John Bill Peter ";
ArrayList<String> list=new ArrayList<String>();
for(String temp:target.split(" "))
{
list.add(temp);
}
Collections.sort(list);
for(String temp:list)
{
System.out.print(temp+" ");
}
}
追问
那如何实现倒序呢?
输出Peter John Bill
追答
public static void main(String args[])
{
String target="John Bill Peter ";
ArrayList list=new ArrayList();
for(String temp:target.split(" "))
{
list.add(temp);
}
Collections.sort(list);
for(String temp:list)
{
System.out.print(temp+" ");
}
System.out.println();
Collections.reverse(list);
for(String temp:list)
{
System.out.print(temp+" ");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询