要求:将扑克牌的数组版改为使用ArrayList集合重新创建牌打牌的三个人,使用ArrayList进行存储,分别通过遍历给三个人发牌
package day02;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class ExerciseDemo {
public static void main(String[] args) {
//创建牌
List<Card> lists=new ArrayList<Card>();
for(int i=0;i<Card.RANK_NAMES.length-2;i++){
lists.add(new Card(0,i));
lists.add(new Card(1,i));
lists.add(new Card(2,i));
lists.add(new Card(3,i));
}
lists.add(new Card(4,Card.RANK_NAMES.length-2));
lists.add(new Card(4,Card.RANK_NAMES.length-1));
//洗牌
Collections.shuffle(lists);
// System.out.println("洗完的牌是:"+lists);
//发牌
Persons[] p=new Persons[3];
p[0]=new Persons("zs");
p[1]=new Persons("lisi");
p[2]=new Persons("ww");
for(int i=0;i<lists.size();i++){
p[i%3].getList().add(lists.get(i));
}
// Iterator<Card> it=lists.iterator();
// while(it.hasNext()){
// System.out.println(it.next());
// }
System.out.println("=====第九十届漂三页大赛开始=====");
System.out.println("zs的牌是:"+p[0].getList().subList(0, 3));
System.out.println("lisi的牌是:"+p[1].getList().subList(0, 3));
System.out.println("ww的牌是:"+p[2].getList().subList(0, 3));
System.out.println("本次大赛的获胜者是:"+"-----");
//关于如何打牌,不讨论,自定义集合排序规则就可以,比较麻烦
}
}
class Persons{
private String name;
private ArrayList<Card> list;
public ArrayList<Card> getList() {
return list;
}
public void setList(ArrayList<Card> list) {
this.list = list;
}
/**
* @param name
*/
public Persons(String name) {
this.setName(name);
list=new ArrayList<Card>();
}
public Persons() {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}