如Collections.sort(List<T> list),Collections.sort(List<T>list, Comparator<? super T> c)。
使用sort方法可以根据元素的自然顺序对指定列表按升序进行排序。列表中的所有元素都必须实现Comparable接口,而且必须是使用指定比较器可相互比较的。
ArrayList<String> poker=new ArrayList<>();
//定义两个数组集合分别存储花色和数字
String[] colors={"♠","♥","♣","♦"};
String[] number={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//增强for循环形成牌
for (String color : colors) {
for (String num : number) {
poker.add(color+num);
}
}
//把大小王加进去
poker.add("大王");
poker.add("小王");
long end1=System.currentTimeMillis();
System.out.println("====准备牌完毕,用时"+(end1-start1)+"毫秒,洗牌开始!====");
//用Collections工具类中混排的方法,进行洗牌
Collections.shuffle(poker);
如Collections.reverse(List<?>list)
使用reverse()反转集合中元素的顺序
根据指定比较器产生的顺序,返回给定Collection的最小(大)元素。
min(Collection),min(Collection,Comparator)
max(Collection),max(Collection,Comparator)
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |