黑马程序员全套Java教程_Java基础教程_集合进阶之Collections(二十九)
6.1 Collections概述和使用
Collections类的概述:是针对集合操作的工具类。 Collections类的常用方法: (1)public static <T extends Comparable<? super T>> void sort(List list):将指定的列表按升序排序; (2)public static void reverse(List<?> list):反转指定列表中元素的顺序; (3)public static void shuffle(List<?> list):使用默认的随机源随机排列指定的列表。
public static void main ( String [ ] args) {
ArrayList < String > arrayList = new ArrayList < > ( ) ;
arrayList. add ( "liubei" ) ;
arrayList. add ( "zhangfei" ) ;
arrayList. add ( "guanyu" ) ;
System . out. println ( arrayList) ;
Collections . sort ( arrayList) ;
System . out. println ( arrayList) ;
Collections . reverse ( arrayList) ;
System . out. println ( arrayList) ;
Collections . shuffle ( arrayList) ;
System . out. println ( arrayList) ;
}
案例:ArrayList存储学生对象并排序
需求:ArrayList存储学生对象,使用Collections对ArrayList进行排序。要求按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序。 思路: (1)定义学生类; (2)创建ArrayList集合; (3)创建学生对象; (4)把学生对象添加到集合; (5)使用Collections对ArrayList集合排序; (6)遍历集合。
public static void main ( String [ ] args) {
List < Student > arrayList = new ArrayList < > ( ) ;
arrayList. add ( new Student ( "liubei" , 13 ) ) ;
arrayList. add ( new Student ( "guanyu" , 1 ) ) ;
arrayList. add ( new Student ( "zhangfei" , 50 ) ) ;
arrayList. add ( new Student ( "liushan" , 50 ) ) ;
for ( Student s : arrayList) {
System . out. println ( s) ;
}
Collections . sort ( arrayList, new Comparator < Student > ( ) {
@Override
public int compare ( Student s1, Student s2) {
int num = s1. getAge ( ) - s2. getAge ( ) ;
return num== 0 ? s1. getName ( ) . compareTo ( s2. getName ( ) ) : num;
}
} ) ;
for ( Student s : arrayList) {
System . out. println ( s) ;
}
}
案例:模拟斗地主
需求:通过程序实现斗地主过程中的洗牌,发牌和看牌。 思路: (1)创建一个牌盒,也就是定义一个集合对象,用ArrayList集合实现; (2)往牌盒里面装牌; (3)洗牌,也就是把牌打散,用Collections的shuffle()实现 (4)发牌,也就是遍历集合,给三个玩家发牌; (5)看牌,也就是三个玩家分别遍历自己的牌。
public static void main ( String [ ] args) {
ArrayList < String > arrayList = new ArrayList < > ( ) ;
String [ ] colors = { "♦" , "♣" , "♥" , "♠" } ;
String [ ] numbers = { "A" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" } ;
for ( String color : colors) {
for ( String number : numbers) {
arrayList. add ( color + number) ;
}
}
arrayList. add ( "大王" ) ;
arrayList. add ( "小王" ) ;
Collections . shuffle ( arrayList) ;
ArrayList < String > liubei = new ArrayList < > ( ) ;
ArrayList < String > guanyu = new ArrayList < > ( ) ;
ArrayList < String > zhangfei = new ArrayList < > ( ) ;
ArrayList < String > hand = new ArrayList < > ( ) ;
for ( int i = 0 ; i < arrayList. size ( ) ; i++ ) {
if ( i >= arrayList. size ( ) - 3 ) {
hand. add ( arrayList. get ( i) ) ;
} else if ( i % 3 == 0 ) {
liubei. add ( arrayList. get ( i) ) ;
} else if ( i % 3 == 1 ) {
guanyu. add ( arrayList. get ( i) ) ;
} else if ( i % 3 == 2 ) {
zhangfei. add ( arrayList. get ( i) ) ;
}
}
lookCards ( liubei) ;
lookCards ( guanyu) ;
lookCards ( zhangfei) ;
lookCards ( hand) ;
}
public static void lookCards ( ArrayList < String > arrayList) {
for ( String s : arrayList) {
System . out. print ( s + " " ) ;
}
System . out. println ( ) ;
}
案例:模拟斗地主升级版
需求:通过程序实现斗地主过程中的洗牌,发牌和看牌。要求对牌进行排序。 分析: 思路: (1)创建HashMap,键是编号,值是牌; (2)创建ArrayList,存储编号; (3)创建花色数组和点数数组; (4)从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号; (5)洗牌(洗的是编号),用Collections的shuffle()实现; (6)发牌(发的也是编号,为了保证编号是排序的,创建TreeSet集合接收); (7)定义方法看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌); (8)调用方法看牌。
public static void main ( String [ ] args) {
HashMap < Integer , String > hs = new HashMap < > ( ) ;
ArrayList < Integer > arrayList = new ArrayList < > ( ) ;
String [ ] colors = { "♦" , "♣" , "♥" , "♠" } ;
String [ ] numbers = { "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" , "2" } ;
int count = 0 ;
for ( String number : numbers) {
for ( String color : colors) {
arrayList. add ( count) ;
hs. put ( count, color + number) ;
count++ ;
}
}
hs. put ( count++ , "小王" ) ;
arrayList. add ( 52 ) ;
hs. put ( count, "大王" ) ;
arrayList. add ( 53 ) ;
Collections . shuffle ( arrayList) ;
TreeSet < Integer > player1 = new TreeSet ( ) ;
TreeSet < Integer > player2 = new TreeSet ( ) ;
TreeSet < Integer > player3 = new TreeSet ( ) ;
TreeSet < Integer > hand = new TreeSet ( ) ;
for ( int i = 0 ; i < arrayList. size ( ) ; i++ ) {
if ( i >= arrayList. size ( ) - 3 ) {
hand. add ( arrayList. get ( i) ) ;
} else if ( i % 3 == 0 ) {
player1. add ( arrayList. get ( i) ) ;
} else if ( i % 3 == 1 ) {
player2. add ( arrayList. get ( i) ) ;
} else if ( i % 3 == 2 ) {
player3. add ( arrayList. get ( i) ) ;
}
}
System . out. print ( "玩家1:" ) ;
lookCards ( player1, hs) ;
System . out. print ( "玩家2:" ) ;
lookCards ( player2, hs) ;
System . out. print ( "玩家3:" ) ;
lookCards ( player3, hs) ;
System . out. print ( "底牌:" ) ;
lookCards ( hand, hs) ;
}
public static void lookCards ( TreeSet < Integer > ts, HashMap < Integer , String > hm) {
for ( int i : ts) {
System . out. print ( hm. get ( i) + " " ) ;
}
System . out. println ( ) ;
}