作者:季沐测试笔记
Collections类的作用
是针对集合操作的工具类
Collections类常用方法
方法名
说明
public static void sort(List list)
将指定的列表按升序排序
public static void reverse(List> list)
反转指定列表中元素的顺序
public static void shuffle(List> list)
使用默认的随机源随机排列指定的列表
示例代码
public class CollectionsDemo01 {
public static void main(String[] args) {
//创建集合对象
List<Integer> list = new ArrayList<Integer>(); //添加元素
list.add(30);
list.add(20);
list.add(50);
list.add(10);
list.add(40);
//public static <T extends Comparable<? super T>> void sort(List<T> list):将指定的列表按升序排序
// Collections.sort(list); //public static void reverse(List<?> list):反转指定列表中元素的顺序
// Collections.reverse(list); //public static void shuffle(List<?> list):使用默认的随机源随机排列指定的列表
Collections.shuffle(list);
System.out.println(list);
}
}
案例需求
代码实现
学生类
public class Student {
private String name;
private int age;public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
测试类
public class CollectionsDemo02 {
public static void main(String[] args) {
//创建ArrayList集合对象
ArrayList<Student> array = new ArrayList<Student>(); //创建学生对象
Student s1 = new Student("linqingxia", 30);
Student s2 = new Student("zhangmanyu", 35);
Student s3 = new Student("wangzuxian", 33);
Student s4 = new Student("liuyan", 33);//把学生添加到集合
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
//使用Collections对ArrayList集合排序
//sort(List&lt;T&gt; list, Comparator&lt;? super T&gt; c)
Collections.sort(array, new Comparator&lt;Student&gt;() {
@Override
public int compare(Student s1, Student s2) {
//按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
int num = s1.getAge() - s2.getAge();
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
return num2;
}
});
//遍历集合
for (Student s : array) {
System.out.println(s.getName() + "," + s.getAge());
}
}
}
案例需求
通过程序实现斗地主过程中的洗牌,发牌和看牌
代码实现
public class PokerDemo {
public static void main(String[] args) {
//创建一个牌盒,也就是定义一个集合对象,用ArrayList集合实现
ArrayList<String> array = new ArrayList<String>(); //往牌盒里面装牌
/*
2,3,4...K,A
2,...
2,...
2,...
小王,大王
*/
//定义花色数组
String[] colors = {"", "", "", ""};
//定义点数数组
String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
for (String color : colors) {
for (String number : numbers) {
array.add(color + number);
}
}
array.add("小王");
array.add("大王");
//洗牌,也就是把牌打撒,用Collections的shuffle()方法实现
Collections.shuffle(array);
// System.out.println(array); //发牌,也就是遍历集合,给三个玩家发牌
ArrayList<String> lqxArray = new ArrayList<String>();
ArrayList<String> lyArray = new ArrayList<String>();
ArrayList<String> fqyArray = new ArrayList<String>();
ArrayList<String> dpArray = new ArrayList<String>();
for (int i = 0; i < array.size(); i++) {
String poker = array.get(i);
if (i >= array.size() - 3) {
dpArray.add(poker);
} else if (i % 3 == 0) {
lqxArray.add(poker);
} else if (i % 3 == 1) {
lyArray.add(poker);
} else if (i % 3 == 2) {
fqyArray.add(poker);
}
}
//看牌,也就是三个玩家分别遍历自己的牌
lookPoker("林青霞", lqxArray);
lookPoker("柳岩", lyArray);
lookPoker("风清扬", fqyArray);
lookPoker("底牌", dpArray);
}
//看牌的方法
public static void lookPoker(String name, ArrayList<String> array) {
System.out.print(name + "的牌是:");
for (String poker : array) {
System.out.print(poker + " ");
}
System.out.println();
}
}
案例需求
通过程序实现斗地主过程中的洗牌,发牌和看牌。要求:对牌进行排序
代码实现
public class PokerDemo {
public static void main(String[] args) {
//创建HashMap,键是编号,值是牌
HashMap<Integer, String> hm = new HashMap<Integer, String>(); //创建ArrayList,存储编号
ArrayList<Integer> array = new ArrayList<Integer>();
//创建花色数组和点数数组
String[] colors = {"", "", "", ""};
String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
//从0开始往HashMap里面存储编号,并存储对应的牌。同时往ArrayList里面存储编号
int index = 0;
for (String number : numbers) {
for (String color : colors) {
hm.put(index, color + number);
array.add(index);
index++;
}
}
hm.put(index, "小王");
array.add(index);
index++;
hm.put(index, "大王");
array.add(index);
//洗牌(洗的是编号),用Collections的shuffle()方法实现
Collections.shuffle(array);
//发牌(发的也是编号,为了保证编号是排序的,创建TreeSet集合接收)
TreeSet<Integer> lqxSet = new TreeSet<Integer>();
TreeSet<Integer> lySet = new TreeSet<Integer>();
TreeSet<Integer> fqySet = new TreeSet<Integer>();
TreeSet<Integer> dpSet = new TreeSet<Integer>();
for (int i = 0; i < array.size(); i++) {
int x = array.get(i);
if (i >= array.size() - 3) {
dpSet.add(x);
} else if (i % 3 == 0) {
lqxSet.add(x);
} else if (i % 3 == 1) {
lySet.add(x);
} else if (i % 3 == 2) {
fqySet.add(x);
}
}
//调用看牌方法
lookPoker("林青霞", lqxSet, hm);
lookPoker("柳岩", lySet, hm);
lookPoker("风清扬", fqySet, hm);
lookPoker("底牌", dpSet, hm);
}
//定义方法看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
public static void lookPoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) {
System.out.print(name + "的牌是:");
for (Integer key : ts) {
String poker = hm.get(key);
System.out.print(poker + " ");
}
System.out.println();
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章