JAVA编写程序,对英文单词按照字母顺序进行排序(升序or降序)
降序排序后PeterJohnBill改下面的,变成降序importjava.util.ArrayList;importjava.util.Collections;publ...
降序排序后 Peter John Bill
改下面的,变成降序
import java.util.ArrayList;
import java.util.Collections;
public class Main {
/**
* @param args the command line arguments
*/
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+" ");
}
}
} 展开
改下面的,变成降序
import java.util.ArrayList;
import java.util.Collections;
public class Main {
/**
* @param args the command line arguments
*/
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+" ");
}
}
} 展开
1个回答
展开全部
Easy!
import java.util.ArrayList;
import java.util.Collections;
public class Main {
/**
* @param args
* the command line arguments
*/
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(int i = list.size(); i > 0; i--){
System.out.println(list.get(i-1) + " ");
}
}
}
----------------
Peter
John
Bill
import java.util.ArrayList;
import java.util.Collections;
public class Main {
/**
* @param args
* the command line arguments
*/
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(int i = list.size(); i > 0; i--){
System.out.println(list.get(i-1) + " ");
}
}
}
----------------
Peter
John
Bill
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询